岛屿和缺口订购问题MYSQL 8.0

问题描述

我正尝试使用partition by&row_number()对给定日期范围内的连续重复值进行计数。本质上,它试图捕获“条纹”。如果条纹出现中断,则计数应在值出现时重新开始再次。

要在此处重现这些结果,请输入代码:

    CREATE TABLE partion_test (
    daily DATE,response_short_name VARCHAR(10)
    
);

INSERT INTO `partion_test` (`daily`,`response_short_name`) VALUES
('2020-09-21','A'),('2020-09-25',('2020-09-26',('2020-09-27',('2020-09-28',('2020-09-22','B'),('2020-09-20','C'),('2020-09-23',('2020-09-24','C');





SELECT 
daily,response_short_name,row_number() over (partition by response_short_name order by daily) as seqnum 
FROM (

select
daily,response_short_name

 FROM partion_test  
order by daily  limit 1000
) A;

当前的输出在这里

|   daily    | response_short_name | seqnum |  |
+------------+---------------------+--------+--+
| 2020-09-21 | A                   |      1 |  |
| 2020-09-25 | A                   |      2 |  |
| 2020-09-26 | A                   |      3 |  |
| 2020-09-27 | A                   |      4 |  |
| 2020-09-28 | A                   |      5 |  |
| 2020-09-22 | B                   |      1 |  |
| 2020-09-20 | C                   |      1 |  |
| 2020-09-23 | C                   |      2 |  |
| 2020-09-24 | C                   |      3 |  |
+------------+---------------------+--------+--+

这里是所需的支出

 +------------+---------------------+--------+--+
|   daily    | response_short_name | seqnum |  |
+------------+---------------------+--------+--+
| 2020-09-20 | C                   |      1 |  |
| 2020-09-21 | A                   |      1 |  |
| 2020-09-22 | B                   |      1 |  |
| 2020-09-23 | C                   |      1 |  |
| 2020-09-24 | C                   |      2 |  |
| 2020-09-25 | A                   |      1 |  |
| 2020-09-26 | A                   |      2 |  |
| 2020-09-27 | A                   |      3 |  |
| 2020-09-28 | A                   |      4 |  |
+------------+---------------------+--------+--+

对此我已经摸了一段时间。任何帮助将不胜感激

解决方法

您可以这样做:

filteredListRentals = (ArrayList<Rental>) listOfRentals.stream()
    .filter(n -> n instanceof Game || n instanceof Console)
    .sorted((n1,n2) -> n1.getNameOfRenter().compareTo(n2.nameOfRenter))
    .collect(Collectors.toList());

结果:

select *,row_number() over(partition by grp order by daily) as seqnum  
from (
  select *,sum(inc) over(order by daily) as grp
  from (
    select *,case when lag(response_short_name) over(order by daily) = response_short_name
        then 0 else 1 end as inc
    from partion_test
    order by daily
  ) x
) y
order by daily

请参见DB Fiddle上的运行示例:

,

您的数据不符合您的结果,因此很难达到您的结果

    CREATE TABLE partion_test (
    daily DATE,response_short_name VARCHAR(10)
    
);

INSERT INTO `partion_test` (`daily`,`response_short_name`) VALUES
('2020-09-21','A'),('2020-09-25',('2020-09-26',('2020-09-27',('2020-09-28',('2020-09-22','B'),('2020-09-20','C'),('2020-09-23',('2020-09-24','C');
select `daily`,`response_short_name`,row_number() over (partition by `response_short_name`,grp order by `daily`) as row_num
from (select t.*,(row_number() over (order by `daily`) -
              row_number() over (partition by `response_short_name` order by `daily`)
             ) as grp
      from partion_test t
     ) t
     ORDER BY `daily`
daily      | response_short_name | row_num
:--------- | :------------------ | ------:
2020-09-20 | C                   |       1
2020-09-21 | A                   |       1
2020-09-22 | B                   |       1
2020-09-23 | C                   |       1
2020-09-24 | C                   |       2
2020-09-25 | A                   |       1
2020-09-26 | A                   |       2
2020-09-27 | A                   |       3
2020-09-28 | A                   |       4

db 提琴here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...