我有一张桌子,如果它们在特定时间段内,我想对其进行分类
我现在所拥有的:
SELECT RelatieId,
AccountId,
[UaId],
[VisitDate],
[Titel],
[Editie],
CASE
WHEN DATEDIFF(SECOND, v.LastVisit, v.VisitDate) > (10 * 60)
THEN 0
ELSE 1
END AS reset
FROM rfv.click v
结果是这样的
RelatieId AccountId UaId VisitDate Titel reset
70409280 120211 8408 2019-04-01 09:15:52.000 Nx 1
70409280 120211 14531 2019-04-01 11:45:41.000 Nx 0
70409280 120211 14531 2019-04-01 11:45:55.000 Nx 1
70409280 120211 14531 2019-04-01 11:46:10.000 Nx 1
70409280 120211 14531 2019-04-01 11:47:16.000 Nx 1
70409280 120211 14531 2019-04-01 11:52:52.000 Nx 1
70409280 120211 14531 2019-04-01 11:53:14.000 Nx 1
70409280 120211 14531 2019-04-01 12:02:44.000 Nx 1
70409280 120211 14531 2019-04-01 12:03:08.000 Nx 1
70409280 120211 14531 2019-04-02 08:06:42.000 Nx 0
70409280 120211 14531 2019-04-02 08:07:07.000 Nx 1
70409280 120211 14531 2019-04-02 08:36:15.000 Nx 0
70409280 120211 14531 2019-04-02 08:36:26.000 Nx 1
但是我真正需要的是
RelatieId AccountId UaId VisitDate Titel needed
70409280 120211 8408 2019-04-01 09:15:52.000 Nx 1
70409280 120211 14531 2019-04-01 11:45:41.000 Nx 2
70409280 120211 14531 2019-04-01 11:45:55.000 Nx 3
70409280 120211 14531 2019-04-01 11:46:10.000 Nx 3
70409280 120211 14531 2019-04-01 11:47:16.000 Nx 3
70409280 120211 14531 2019-04-01 11:52:52.000 Nx 3
70409280 120211 14531 2019-04-01 11:53:14.000 Nx 3
70409280 120211 14531 2019-04-01 12:02:44.000 Nx 3
70409280 120211 14531 2019-04-01 12:03:08.000 Nx 3
70409280 120211 14531 2019-04-02 08:06:42.000 Nx 4
70409280 120211 14531 2019-04-02 08:07:07.000 Nx 5
70409280 120211 14531 2019-04-02 08:36:15.000 Nx 6
70409280 120211 14531 2019-04-02 08:36:26.000 Nx 7
答案 0 :(得分:1)
我不确定何时要重置此值(从1开始),例如针对不同的RelatieId,针对不同的AccountId还是针对两者。您需要同时调整PARTITION BY
和LAG
窗口函数中的SUM
。这是基本版本:
;WITH resetChangeCTE AS
(
SELECT *
, IIF(reset != LAG(reset, 1, 0) OVER (PARTITION BY RelatieId, AccountId ORDER BY VisitDate), 1, 0) AS hasResetChanged
FROM (VALUES (70409280, 120211, 8408 , CONVERT(DATETIME, '2019-04-01 09:15:52.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:45:41.000'), 0),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:45:55.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:46:10.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:47:16.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:52:52.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 11:53:14.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 12:02:44.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-01 12:03:08.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-02 08:06:42.000'), 0),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-02 08:07:07.000'), 1),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-02 08:36:15.000'), 0),
(70409280, 120211, 14531, CONVERT(DATETIME, '2019-04-02 08:36:26.000'), 1)) x(RelatieId , AccountId , UaId ,VisitDate ,reset)
)
SELECT *, SUM(hasResetChanged) OVER (PARTITION BY RelatieId, AccountId ORDER BY VisitDate) AS needed
FROM resetChangeCTE
因此,我首先检查reset
值在当前记录和上一个记录之间是否发生了变化(使用LAG
,按访问日期排序),然后将变化量求和从第一个记录到使用SUM
的当前记录。这些是window functions,非常有用。
答案 1 :(得分:0)
我认为您确实想要:
SELECT c.*,
SUM(CASE WHEN DATEDIFF(SECOND, v.LastVisit, v.VisitDate) > (10 * 60)
THEN 0
ELSE 1
END) AS needed
FROM (SELECT c.*,
LAG(VisitDate) OVER (PARTITION BY RelatieId, AccountId, UaId ORDER BY VisitDate) as LastVisit
FROM rfv.click c
) c;
这不会完全返回您指定的结果。但是,这似乎更有用,可能正是您真正想要的。