gpt4 book ai didi

sql - 时间序列数据的孤岛和间隙问题

转载 作者:行者123 更新时间:2023-12-02 18:58:06 26 4
gpt4 key购买 nike

我有一个示例数据,我一直在尝试获取所需的数据,如下所示。我也能够实现某种岛屿和间隙解决方案。这是我达到的最接近预期数据的版本。

DROP TABLE IF EXISTS #data
CREATE TABLE #data(
factor varchar(50),
val int,
[start_date] date, [end_date] date
)
Go
INSERT INTO #data VALUES
('a', 15, '2021-01-01', '2021-01-05'),
('a', 15, '2021-01-08', '2021-01-10'),
('a', 20, '2021-01-11', '2021-01-20'),
('a', 15, '2021-01-21', '2099-01-01'),
('b', 10, '2021-01-01', '2021-01-04'),
('b', 12, '2021-01-05', '2021-01-13'),
('b', 12, '2021-01-17', '2021-01-19'),
('b', 12, '2021-01-20', '2021-01-23'),
('b', 10, '2021-01-24', '2099-01-01');

WITH value_cte As (
SELECT * ,
RANK() OVER(PARTITION BY factor ORDER BY [start_date]) - RANK() OVER(PARTITION BY factor, val ORDER BY [start_date]) grp
FROM #data

)
SELECT factor, val, MIN(start_date) st, MAX(end_date) ed
FROM value_cte
GROUP BY factor, val, grp
ORDER BY factor, st

以上查询的结果:

result achieved

预期结果:

factor  val st          ed
a 15 2021-01-01 2021-01-05
a 15 2021-01-08 2021-01-10
a 20 2021-01-11 2021-01-20
a 15 2021-01-21 2099-01-01
b 10 2021-01-01 2021-01-04
b 12 2021-01-05 2021-01-13
b 12 2021-01-17 2021-01-23
b 10 2021-01-24 2099-01-01

即使两个连续岛的值相同并且存在间隙,则该间隙不应合并,如果两个岛连续则应合并。不幸的是,我无法更改此处的源(示例数据结构)

最佳答案

您可以使用lag()来确定“岛屿”的起始位置——即没有重叠的位置。然后使用基于日期算术的累积和:

select factor, val, min(start_date), max(end_date)
from (select d.*,
sum(case when prev_end_date >= dateadd(day, -1, start_date) then 0 else 1 end) over (partition by factor, val order by start_date) as grp
from (select d.*,
lag(end_date) over (partition by factor, val order by start_date) as prev_end_date
from data d
) d
) d
group by factor, val, grp
order by factor, min(start_date);

Here是一个 SQL Fiddle。

关于sql - 时间序列数据的孤岛和间隙问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65940060/

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