更新上一行的开始和结束日期以确保没有日期间隔

问题描述 投票:0回答:0

我有一张交易表

tbl.transactions
,我们的投资组合的收费会议看起来像这样。

session_id 站名 station_id port_no session_sd session_ed 没有端口
1000 东 Sp 15 A:10 1 2022-11-10 18:24:44.000 2022-11-10 21:09:20.000 2
1001 东 Sp 15 A:10 2 2022-11-11 18:05:47.000 2022-11-11 22:44:02.000 2
1070 东 Sp 15 A:10 2 2022-11-27 18:16:03.000 2022-11-27 23:45:03.000 2
1075 E - 空间 15 A:10 1 2022-12-06 17:07:53.000 2022-12-06 17:54:36.000 2
1082 东 Sp 15 A:10 1 2022-12-10 10:19:56.000 2022-12-10 15:22:23.000 2
1083 棕色 - 第 2 批 B:20 1 2023-01-14 15:37:15.000 2023-01-14 17:51:43.000 2
1086 棕色 - 第 2 批 B:20 2 2023-01-21 18:00:35.000 2023-01-21 22:21:34.000 2

我正在尝试创建一个表来显示每个站的开始和结束日期(这样我就可以加入日历表并最终按小时确定利用率)。这些日期反映了从 API 中提取每个电台名称的时间段,即电台的可用性。根据数据的结构,station_name 可以由管理团队更新/更改,station_id 也可能更改或移动位置,如上所示。站也可以有 1 个或 2 个端口。我对其他方法持开放态度,但我能想到的最好方法是为历史车站创建一个看起来像这样的表格。

station_id 站名 port_no station_start station_end
A:10 东 Sp 15 1 2022-11-10 18:24:44.000 2022-11-27 23:45:03.000
A:10 东 Sp 15 2 2022-11-10 18:24:44.000 2022-11-27 23:45:03.000
A:10 E - 空间 15 1 2022-11-27 23:45:04.000 2022-12-06 17:07:54.000
A:10 E - 空间 15 2 2022-11-27 23:45:04.000 2022-12-06 17:07:54.000
A:10 东 Sp 15 1 2022-12-06 17:07:55.000 明天的约会
A:10 东 Sp 15 2 2022-12-06 17:07:55.000 明天的约会
B:20 棕色 - 第 2 批 1 2023-01-14 15:37:15.000 明天的约会
B:20 棕色 - 第 2 批 2 2023-01-14 15:37:15.000 明天的约会

我不担心“明天的约会”。我如何更新每个后续的

station_start
station_end
以反映每个 station_id/station_name 可能进行会话的时间段?

这是我的查询,我得到的最接近的是在使用

start_dt_adj
LAG
LEAD
列中。

with number_tbl as (
    SELECT 1 as Number
    UNION ALL
    SELECT Number + 1 FROM number_tbl where Number <=2
    )
SELECT * INTO #no_tbl 
FROM number_tbl

SELECT  DISTINCT x.station_id
        ,x.station_name
        ,x.port_no
        ,x.no_of_ports
        ,x.station_start
        ,x.station_end
        ,CASE   WHEN DATEADD(s, -1, LEAD(station_start, 1) OVER(PARTITION BY station_id, port_no ORDER BY station_id, port_no desc, station_start, station_name)) < station_start
                    THEN DATEADD(s, -1, LEAD(station_start, 1) OVER(PARTITION BY station_id, port_no ORDER BY station_id, port_no desc, station_start, station_name))
                WHEN DATEADD(s, 1, LAG(station_end, 1) OVER(PARTITION BY station_id, port_no ORDER BY station_id, port_no desc, station_end, station_name)) IS NOT NULL
                    THEN DATEADD(s, 1, LAG(station_end, 1) OVER(PARTITION BY station_id, port_no ORDER BY station_id, port_no desc, station_end, station_name))
                ELSE station_start END AS start_dt_adj
FROM    (
        SELECT  DISTINCT d.station_id, d.station_name, d.port_no, d.no_of_ports, a.station_start, a.station_end
        FROM    tbl.transactions d
                LEFT JOIN
                            (
                            SELECT DISTINCT station_id, station_name
                                    ,MIN(session_sd) 'station_start'
                                    ,MAX(session_ed) 'station_end'
                            FROM tbl.transactions
                            GROUP BY station_id, station_name
                            ) a
                    ON d.station_id = a.station_id
                    AND d.station_name = a.station_name
        ) x
        JOIN #no_tbl n
        ON x.no_of_ports >= n.Number
sql sql-server
相关问题
热门问答
最新问题