0

我有两列的表:年和月。

例子:

Year    |     Month
1. 2020            9
2. 2020            8
3. 2020            7
4. 2020            5
5. 2020            4

我没有 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

我想得到结果:1,2,3,4,5,6

4

1 回答 1

0

一种方法是:

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;

这会将错过的次数加起来到给定的年/月组合。它返回没有缺失月份的行。

是一个显示两个示例的 db<>fiddle。

于 2020-09-14T11:43:42.180 回答