我试图解决的问题是我需要知道 EmployeeId + TenantId (1A) 在连续运行中出现了多少次。我真的只关心它是否大于 1。
我的应用程序在工作日运行并插入行。它记录当前运行日期/时间,并且还添加上次运行的日期和时间(显然是从不同的表检索的)。
CREATE TABLE EmployeeHistory
(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
CurrentRun TEXT,
LastRun TEXT,
EmployeeId TEXT,
TenantId TEXT
);
DELETE FROM EmployeeHistory;
INSERT INTO EmployeeHistory ('CurrentRun', 'LastRun', 'EmployeeId', 'TenantId')
VALUES
-- August 19th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '1', 'A' ), -- Consecutive! (Employee exists in August 18th run)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '3', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '1', 'B' ), -- Should not be included because was not in August 18th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '2', 'B' ), -- Consecutive! (Employee exists in August 18th run)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '4', 'A' ), -- Consecutive! (Employee exists in all runs)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '2', 'A' ), -- Consecutive! (Employee exists in August 18th run)
-- August 18th run
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '1', 'A' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '2', 'A' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '2', 'B' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '4', 'A' ), -- Consecutive! (Employee exists in all runs)
-- August 17th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '3', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '5', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '6', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '4', 'A' ); -- Consecutive! (Employee exists in all runs)
电流输出:
ID CurrentRun LastRun EmployeeId TenantId
1 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 1 A
2 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 3 A
3 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 1 B
4 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 2 B
5 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 4 A
6 2023-08-19 00:00:00.000000 2023-08-18 00:00:00.000000 2 A
7 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 1 A
8 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 2 A
9 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 2 B
10 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 4 A
11 2023-08-17 00:00:00.000000 2023-08-17 00:00:00.000000 3 A
12 2023-08-17 00:00:00.000000 2023-08-17 00:00:00.000000 5 A
13 2023-08-17 00:00:00.000000 2023-08-17 00:00:00.000000 6 A
14 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 4 A
我用谷歌搜索并阅读了一些有关 LEAD 和 LAG 的内容,但我不知道如何取回我想要的数据。
我想到的一个解决方案是添加一列来跟踪连续发生的情况,并根据上次运行简单地增加给定员工的总和。这是一个不好的方法吗?
CREATE TABLE EmployeeHistory
(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
CurrentRun TEXT,
LastRun TEXT,
EmployeeId TEXT,
TenantId TEXT
);
INSERT INTO EmployeeHistory ('CurrentRun', 'LastRun', 'EmployeeId', 'TenantId')
VALUES
-- August 19th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '1', 'A' ), -- Consecutive! (Employee exists in August 18th run)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '3', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '1', 'B' ), -- Should not be included because was not in August 18th run
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '2', 'B' ), -- Consecutive! (Employee exists in August 18th run)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '4', 'A' ), -- Consecutive! (Employee exists in all runs)
( '2023-08-19 00:00:00.000000', '2023-08-18 00:00:00.000000', '2', 'A' ), -- Consecutive! (Employee exists in August 18th run)
-- August 18th run
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '1', 'A' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '2', 'A' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '2', 'B' ), -- Consecutive (Employee exists in August 19th run)!
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '4', 'A' ), -- Consecutive! (Employee exists in all runs)
-- August 17th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '3', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '5', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-17 00:00:00.000000', '2023-08-17 00:00:00.000000', '6', 'A' ), -- Should not be included because was not in August 18th run
( '2023-08-18 00:00:00.000000', '2023-08-17 00:00:00.000000', '4', 'A' ); -- Consecutive! (Employee exists in all runs);
WITH ConsecutiveRuns AS (
SELECT
a.CurrentRun AS CurrentRunDate,
b.CurrentRun AS LastRunDate,
a.EmployeeId,
a.TenantId
FROM
EmployeeHistory a
JOIN EmployeeHistory b
ON
a.LastRun = b.CurrentRun AND
a.EmployeeId = b.EmployeeId AND
a.TenantId = b.TenantId
)
SELECT
EmployeeId,
TenantId,
COUNT(*) AS ConsecutiveCount
FROM
ConsecutiveRuns
GROUP BY
EmployeeId,
TenantId
HAVING
ConsecutiveCount > 1;
|员工 ID |租户 ID |连续计数 | | :------------|:---------|:-----------------| | 4 |一个 | 2 | 小提琴