[英]Is there a way to return all records where the next record on a specific column has a value from previous record + 1?
I have table with 2 columns: Year And Month.我有 2 列的表格:年和月。
Example:例子:
Year | Month
1. 2020 9
2. 2020 8
3. 2020 7
4. 2020 5
5. 2020 4
I don,t have record for 06.2020, so as a result i want to get only all the results before the break: 1,2,3我没有 06.2020 的记录,因此我只想在休息前获得所有结果:1,2,3
Example 2:
1. 2020 12
2. 2020 11
3. 2020 10
4. 2020 09
5. 2020 08
6. 2020 07
7. 2020 05
8. 2020 04
9. 2020 02
I want to get as result: 1,2,3,4,5,6
我想得到结果: 1,2,3,4,5,6
One method is:一种方法是:
select t.*
from (select t.*,
sum(case when next_yyyymm <> year * 12 + month + 1 then 1 else 0 end) over (order by year desc, month desc) as num_misses
from (select t.*,
lead(year * 12 + month) over (order by year, month) as next_yyyymm
from t
) t
) t
where num_misses = 0;
This adds up the number of misses up to a given year/month combination.这会将未命中数加起来达到给定的年/月组合。 It returns the rows with no missing month.它返回没有丢失月份的行。
Here is a db<>fiddle showing both examples. 这是一个显示两个示例的 db<>fiddle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.