gpt4 book ai didi

sql - 如何查找 Oracle 表中的月份间隔?

转载 作者:行者123 更新时间:2023-12-01 12:17:20 25 4
gpt4 key购买 nike

我有一个 Oracle 表,其中包含 EmpName(字符)、Month_from 和 Month_to 列(数字)。在这里我需要找到缺失的月份(月份差距)。在下面的示例数据中,我必须找到缺失的第 6 个月(6 月)。

提前致谢。

Sample Data:

|-------|-----------|--------|
|eName |Month_From |Month_To|
|(Char) | ( Int) | ( Int) |
|-------|------------|-------|
|John |1 |2 | ( Jan to Feb)
|John |3 |5 | ( Mar to May)
|John |7 |8 | ( Jul to Aug)
|-------|------------|-------|

需要查找(Jun 到 Jun)。

最佳答案

假设没有重叠,您可以使用 lag() 找到缺失的月份:

select (prev_month_to + 1) as start_missing,
(month_from - 1) as end_missing
from (select t.*, lag(month_to) over (partition by name order by month_from) as prev_month_to
from t
) t
where prev_month_to <> month_from - 1;

这为每个差距提供了一个范围,因为差距可能超过一个月。

关于sql - 如何查找 Oracle 表中的月份间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com