我有一个带有 id 和 number_col 的项目表。 Stackoverflow 上有很多关于查找 ID 中的空白的问题,但我要查找的是数字中的空白。例如,如果我有 3 件物品:
id | number_col
===============
1 | 1
1 | 2
1 | 4
我需要一个 SQL 查询,该查询返回 id = 1, number_col = 3 作为缺失。查询应该查看每个 id 的 number_col 中的最大值。由于在我的示例中 4 是 id 1 的最大 number_col 值,因此它不应返回缺少 1、5 的情况。
如果您想要缺失值的范围,那么:
select number_col + 1 as first_missing, (next_nc - 1) as last_missing
from (select t.*, lead(number_col) over (partition by id order by number_col) as next_nc
from t
) t
where next_nc <> number_col + 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)