Select 随着时间的推移,每个不同的值都不会丢失其间的 NULL
Select each distinct value over time without losing NULLs in between
让我们假设一个 table 有很多列,一个 Temporal table 正在记录它的历史。有一个字段我需要知道它何时更改。
Number
VersionStartDate
991281
2021-11-12 08:27:11
991281
2021-11-12 08:20:11
NULL
2021-11-12 07:20:11
NULL
2021-11-12 06:20:11
771281
2021-11-11 08:26:11
NULL
2021-11-11 08:25:11
661281
2021-11-10 08:24:11
NULL
2021-11-10 08:22:11
661281
2021-11-10 08:21:11
551281
2021-11-09 08:20:11
我需要获取每个值,以及它发生变化的时刻。我还需要知道它是否已设置为 NULL,因此此查询未提供我需要的信息。
SELECT
Number,
MIN(VersionStartDate) [Date]
FROM _TABLE_
GROUP BY
Number
ORDER BY
[Date] DESC
结果应该是
Number
VersionStartDate
991281
2021-11-12 08:20:11
NULL
2021-11-12 06:20:11
771281
2021-11-11 08:26:11
NULL
2021-11-11 06:25:11
661281
2021-11-10 08:24:11
NULL
2021-11-10 08:22:11
661281
2021-11-10 08:21:11
551281
2021-11-09 08:20:11
在您的组中添加 VersionStartDate。
SELECT
Number,
MIN(VersionStartDate)
FROM _TABLE_
GROUP BY
Number, VersionStartDate
ORDER BY
VersionStartDate DESC
好吧,这是一种方法,我相信还有更多 eloquent 的方法来编写它,但它可以帮助您入门:
CREATE TABLE #T(Number int, VersionStartDate datetime)
INSERT INTO #T vALUES
(991281,'2021-11-12 08:27:11'),
(991281,'2021-11-12 08:20:11'),
(NULL,'2021-11-12 07:20:11'),
(NULL,'2021-11-12 06:20:11'),
(771281,'2021-11-11 08:26:11'),
(NULL,'2021-11-11 08:25:11'),
(661281,'2021-11-10 08:24:11'),
(NULL,'2021-11-10 08:22:11'),
(661281,'2021-11-10 08:21:11'),
(551281,'2021-11-09 08:20:11')
SELECT Number, MIN(VersionStartDate) VersionStartDate
FROM
(
SELECT *, SUM(CASE WHEN ISNULL(Number,-1) <> ISNULL(LG,-1) THEN 1 ELSE 0 END) OVER(ORDER BY VersionStartDate desc) GRP
FROM
(
SELECT *, LAG(Number,1,-1) OVER(ORDER BY VersionStartDate desc) LG
FROM #T
) X
) Y
GROUP BY GRP,Number
ORDER BY VersionStartDate desc
和我开始研究后出现的JMabee很相似,但可能更简单一点:
CREATE TABLE #d (Number INT, VersionStartDate DATETIME);
INSERT INTO #d(Number, VersionStartDate)
VALUES
(991281 ,'2021-11-12T08:27:11'),
(991281 ,'2021-11-12T08:20:11'),
(NULL ,'2021-11-12T07:20:11'),
(NULL ,'2021-11-12T06:20:11'),
(771281 ,'2021-11-11T08:26:11'),
(NULL ,'2021-11-11T08:25:11'),
(661281 ,'2021-11-10T08:24:11'),
(NULL ,'2021-11-10T08:22:11'),
(661281 ,'2021-11-10T08:21:11'),
(551281 ,'2021-11-09T08:20:11');
WITH cte AS
(
SELECT Number,
VersionStartDate,
LAG(Number, 1) OVER (ORDER BY VersionStartDate) AS PrevNumber
FROM #d
)
SELECT cte.Number,
cte.VersionStartDate
FROM cte
WHERE ISNULL(cte.Number, -1) <> ISNULL(cte.PrevNumber, -1)
ORDER BY cte.VersionStartDate DESC;
让我们假设一个 table 有很多列,一个 Temporal table 正在记录它的历史。有一个字段我需要知道它何时更改。
Number | VersionStartDate |
---|---|
991281 | 2021-11-12 08:27:11 |
991281 | 2021-11-12 08:20:11 |
NULL | 2021-11-12 07:20:11 |
NULL | 2021-11-12 06:20:11 |
771281 | 2021-11-11 08:26:11 |
NULL | 2021-11-11 08:25:11 |
661281 | 2021-11-10 08:24:11 |
NULL | 2021-11-10 08:22:11 |
661281 | 2021-11-10 08:21:11 |
551281 | 2021-11-09 08:20:11 |
我需要获取每个值,以及它发生变化的时刻。我还需要知道它是否已设置为 NULL,因此此查询未提供我需要的信息。
SELECT
Number,
MIN(VersionStartDate) [Date]
FROM _TABLE_
GROUP BY
Number
ORDER BY
[Date] DESC
结果应该是
Number | VersionStartDate |
---|---|
991281 | 2021-11-12 08:20:11 |
NULL | 2021-11-12 06:20:11 |
771281 | 2021-11-11 08:26:11 |
NULL | 2021-11-11 06:25:11 |
661281 | 2021-11-10 08:24:11 |
NULL | 2021-11-10 08:22:11 |
661281 | 2021-11-10 08:21:11 |
551281 | 2021-11-09 08:20:11 |
在您的组中添加 VersionStartDate。
SELECT
Number,
MIN(VersionStartDate)
FROM _TABLE_
GROUP BY
Number, VersionStartDate
ORDER BY
VersionStartDate DESC
好吧,这是一种方法,我相信还有更多 eloquent 的方法来编写它,但它可以帮助您入门:
CREATE TABLE #T(Number int, VersionStartDate datetime)
INSERT INTO #T vALUES
(991281,'2021-11-12 08:27:11'),
(991281,'2021-11-12 08:20:11'),
(NULL,'2021-11-12 07:20:11'),
(NULL,'2021-11-12 06:20:11'),
(771281,'2021-11-11 08:26:11'),
(NULL,'2021-11-11 08:25:11'),
(661281,'2021-11-10 08:24:11'),
(NULL,'2021-11-10 08:22:11'),
(661281,'2021-11-10 08:21:11'),
(551281,'2021-11-09 08:20:11')
SELECT Number, MIN(VersionStartDate) VersionStartDate
FROM
(
SELECT *, SUM(CASE WHEN ISNULL(Number,-1) <> ISNULL(LG,-1) THEN 1 ELSE 0 END) OVER(ORDER BY VersionStartDate desc) GRP
FROM
(
SELECT *, LAG(Number,1,-1) OVER(ORDER BY VersionStartDate desc) LG
FROM #T
) X
) Y
GROUP BY GRP,Number
ORDER BY VersionStartDate desc
和我开始研究后出现的JMabee很相似,但可能更简单一点:
CREATE TABLE #d (Number INT, VersionStartDate DATETIME);
INSERT INTO #d(Number, VersionStartDate)
VALUES
(991281 ,'2021-11-12T08:27:11'),
(991281 ,'2021-11-12T08:20:11'),
(NULL ,'2021-11-12T07:20:11'),
(NULL ,'2021-11-12T06:20:11'),
(771281 ,'2021-11-11T08:26:11'),
(NULL ,'2021-11-11T08:25:11'),
(661281 ,'2021-11-10T08:24:11'),
(NULL ,'2021-11-10T08:22:11'),
(661281 ,'2021-11-10T08:21:11'),
(551281 ,'2021-11-09T08:20:11');
WITH cte AS
(
SELECT Number,
VersionStartDate,
LAG(Number, 1) OVER (ORDER BY VersionStartDate) AS PrevNumber
FROM #d
)
SELECT cte.Number,
cte.VersionStartDate
FROM cte
WHERE ISNULL(cte.Number, -1) <> ISNULL(cte.PrevNumber, -1)
ORDER BY cte.VersionStartDate DESC;