我试图解决的问题是,我需要知道在给定日期(当前运行)开始的连续运行中,已显示了多少次“租户ID + 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-16 00:00:00.000000 3 A
12 2023-08-17 00:00:00.000000 2023-08-16 00:00:00.000000 5 A
13 2023-08-17 00:00:00.000000 2023-08-16 00:00:00.000000 6 A
14 2023-08-18 00:00:00.000000 2023-08-17 00:00:00.000000 4 A
我已经谷歌和阅读了一些关于铅和滞后,但我不知道如何找回我想要的数据。
在我看来,一个解决方案是添加一个列来跟踪连续发生的事件,并根据上一次运行简单地增加给定员工的总和。这是个坏方法吗
2条答案
按热度按时间iswrvxsc1#
您可以简单地使用
EXISTS
子句;这将给予您仅在CurrentRun +/- 1day下出现相同的租户ID +TenantId的示例:你为什么把所有的东西都放在“文本”栏里?您可能需要针对数据类型转换进行调整。
或者.如果你想在输出中保留所有行,但只是标记连续的行:
sc4hvdpw2#
`进入登录历史记录('当前运行','最后运行','登录ID','租户ID')VALUES('2023 - 08 - 19 00:00:00.000000','2023 - 08 - 18 00:00:00.000000',' 1','A'),(' 2023 - 08 - 19 00:00:00.000000 ',' 2023 - 08 - 18 00:00:00.000000','3',' A'),('2023 - 08 - 19 00:00:00.000000','2023 - 08 - 18 00:00:00.000000',' 1','B'),(' 2023 - 08 - 19 00:00:00.000000 ',' 2023 - 08 - 18 00:00:00.000000',' 2',' B'),(' 2023 - 08 - 19 00:00:00.000000',' 2023 - 08 - 18 00:00:00.000000',' 4',' A'),(' 2023 - 08 - 19 00:00:00.000000',' 2023 - 08 - 18 00:00:00.000000',' 2',' A'),(' 2023 - 08 - 18 00:00:00.000000',' 2023 - 08 - 17 00:00:00.000000',' 1',' A'),(' 2023 - 08 - 18 00:00:00.000000',' 2023 - 08 - 17 00:00:00.000000',' 2',' A'),(' 2023 - 08 - 18 00:00:00.000000',' 2023 - 08 - 17 00:00:00.000000',' 2',' B'),(' 2023 - 08 - 18 00:00:00.000000',' 2023 - 08 - 17 00:00:00.000000',' 4',' A'),(' 2023 - 08 - 17 00:00:00.000000',' 2023 - 08 - 16 00:00:00.000000',' 3',' A'),(' 2023 - 08 - 17 00:00:00.000000',' 2023 - 08 - 16 00:00:00.000000',' 5',' A'),(' 2023 - 08 - 17 00:00:00.000000',' 2023 - 08 - 16 00:00:00.000000',' 6',' A'),(' 2023 - 08 - 18 00:00:00.000000',' 2023 - 08 - 17 00:00:00.000000',' 4',' A');
| ID|当前运行|最后运行|EmployeeID| tenantId|
| - -|- -|- -|- -|- -|
| 1| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 1|一|
| 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|一|
| 5| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 4|一|
| 6| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 2|一|
| ID|当前运行|最后运行|EmployeeID| tenantId||:-|:-----------|:--------|:-----------|:---------||1| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 1|一||6| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 2|一||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|一||5| 2023 - 08 - 19 00:00:00.000000| 2023 - 08 - 18 00:00:00.000000| 4|一|fiddle