我试图计算自从使用DATEDIFF
开始批处理以来的天数。如果只有一批产品,我确信我所拥有的产品会起作用,但是当我同时使用它时,它显然不会按照我希望的方式工作
我遇到的问题是有多个批次,每个批次都有不同的开始日期。因此,我需要DATEDIFF
计算,以便在选择使用哪个开始日期时考虑它所查看的特定批次。现在它只是使用表中最早的日期而不管批次。
SELECT
T0.[Batch],
T0.[DateTime],
DATEDIFF(day, (SELECT TOP 1 T0.[DateTime]
FROM t005 T0
ORDER BY T0.[DateTime]), T0.[DateTime]) AS [Days],
MAX(T0.[PG]) AS [PG]
FROM
t005 T0
结果如下:
Batch DateTime Days PG
3160036 2016-09-30 11:30:52.000 5 11.83
3160036 2016-09-28 08:35:50.000 3 11.85
3160036 2016-09-27 11:27:41.000 2 11.92
3160036 2016-09-26 07:33:27.000 1 12.18
3160036 2016-09-25 12:42:08.000 0 18.16
3160039 2016-09-28 08:28:50.000 3 18.4
3160039 2016-09-27 09:29:42.000 2 53.59
3160039 2016-09-27 09:19:32.000 2 52.73
3160039 2016-09-27 09:12:46.000 2 52.99
3160039 2016-09-27 09:05:52.000 2 53.08
3160039 2016-09-27 08:34:59.000 2 47.17
3160051 2016-10-27 12:56:33.000 32 11.31
3160051 2016-10-27 12:42:58.000 32 11.17
3160051 2016-10-24 06:12:35.000 29 11.18
3160051 2016-10-24 05:57:18.000 29 11.13
非常感谢。
答案 0 :(得分:1)
更新:还缺少过滤器+ desc排序......
您是否错过了批次之间的连接?
CREATE TABLE t005 (
Batch int
,[DateTime] datetime2
,pg float
)
INSERT INTO t005 values (3160036, cast('2016-09-30 11:30:52' as datetime2), 11.83)
INSERT INTO t005 values (3160036, cast('2016-09-28 08:35:50' as datetime2), 11.85)
INSERT INTO t005 values (3160036, cast('2016-09-27 11:27:41' as datetime2), 11.92)
INSERT INTO t005 values (3160036, cast('2016-09-26 07:33:27' as datetime2), 12.18)
INSERT INTO t005 values (3160036, cast('2016-09-25 12:42:08' as datetime2), 18.16)
INSERT INTO t005 values (3160039, cast('2016-09-28 08:28:50' as datetime2), 18.4 )
INSERT INTO t005 values (3160039, cast('2016-09-27 09:29:42' as datetime2), 53.59)
INSERT INTO t005 values (3160039, cast('2016-09-27 09:19:32' as datetime2), 52.73)
INSERT INTO t005 values (3160039, cast('2016-09-27 09:12:46' as datetime2), 52.99)
INSERT INTO t005 values (3160039, cast('2016-09-27 09:05:52' as datetime2), 53.08)
INSERT INTO t005 values (3160039, cast('2016-09-27 08:34:59' as datetime2), 47.17)
INSERT INTO t005 values (3160051, cast('2016-10-27 12:56:33' as datetime2), 11.31)
INSERT INTO t005 values (3160051, cast('2016-10-27 12:42:58' as datetime2), 11.17)
INSERT INTO t005 values (3160051, cast('2016-10-24 06:12:35' as datetime2), 11.18)
INSERT INTO t005 values (3160051, cast('2016-10-24 05:57:18' as datetime2), 11.13)
SELECT
T0.[Batch],
T0.[DateTime],
(SELECT TOP 1
T1.[DateTime]
FROM
t005 T1
WHERE T1.Batch = T0.Batch
AND T1.[DateTime] < T0.[DateTime]
ORDER BY T1.[DateTime] DESC) as LastDateTime,
ISNULL(DATEDIFF(day,(SELECT TOP 1
T1.[DateTime]
FROM
t005 T1
WHERE T1.Batch = T0.Batch
AND T1.[DateTime] < T0.[DateTime]
ORDER BY T1.[DateTime] DESC), T0.[DateTime]),0) AS [Days],
T0.[PG] AS [PG]
FROM t005 as T0
ORDER BY T0.[Batch],
T0.[DateTime]
--drop table t005
输出:
Batch |DateTime |LastDateTime |Days |PG
--------|----------------------------|-----------------------------|-----|----
3160036 |2016-09-25 12:42:08.0000000 |NULL |0 |18.16
3160036 |2016-09-26 07:33:27.0000000 |2016-09-25 12:42:08.0000000 |1 |12.18
3160036 |2016-09-27 11:27:41.0000000 |2016-09-26 07:33:27.0000000 |1 |11.92
3160036 |2016-09-28 08:35:50.0000000 |2016-09-27 11:27:41.0000000 |1 |11.85
3160036 |2016-09-30 11:30:52.0000000 |2016-09-28 08:35:50.0000000 |2 |11.83
3160039 |2016-09-27 08:34:59.0000000 |NULL |0 |47.17
3160039 |2016-09-27 09:05:52.0000000 |2016-09-27 08:34:59.0000000 |0 |53.08
3160039 |2016-09-27 09:12:46.0000000 |2016-09-27 09:05:52.0000000 |0 |52.99
3160039 |2016-09-27 09:19:32.0000000 |2016-09-27 09:12:46.0000000 |0 |52.73
3160039 |2016-09-27 09:29:42.0000000 |2016-09-27 09:19:32.0000000 |0 |53.59
3160039 |2016-09-28 08:28:50.0000000 |2016-09-27 09:29:42.0000000 |1 |18.4
3160051 |2016-10-24 05:57:18.0000000 |NULL |0 |11.13
3160051 |2016-10-24 06:12:35.0000000 |2016-10-24 05:57:18.0000000 |0 |11.18
3160051 |2016-10-27 12:42:58.0000000 |2016-10-24 06:12:35.0000000 |3 |11.17
3160051 |2016-10-27 12:56:33.0000000 |2016-10-27 12:42:58.0000000 |0 |11.31
从第一次执行批处理开始的查询持续时间:
SELECT
T0.[Batch],
T0.[DateTime],
(SELECT TOP 1
T1.[DateTime]
FROM
t005 T1
WHERE T1.Batch = T0.Batch
AND T1.[DateTime] < T0.[DateTime]
ORDER BY T1.[DateTime] ASC) as LastDateTime,
ISNULL(DATEDIFF(day,(SELECT TOP 1
T1.[DateTime]
FROM
t005 T1
WHERE T1.Batch = T0.Batch
AND T1.[DateTime] < T0.[DateTime]
ORDER BY T1.[DateTime] ASC), T0.[DateTime]),0) AS [Days],
T0.[PG] AS [PG]
FROM t005 as T0
ORDER BY T0.[Batch],
T0.[DateTime]
输出:
Batch |DateTime |LastDateTime |Days |PG
--------|----------------------------|----------------------------|------|-------
3160036 |2016-09-25 12:42:08.0000000 |NULL |0 |18,16
3160036 |2016-09-26 07:33:27.0000000 |2016-09-25 12:42:08.0000000 |1 |12,18
3160036 |2016-09-27 11:27:41.0000000 |2016-09-25 12:42:08.0000000 |2 |11,92
3160036 |2016-09-28 08:35:50.0000000 |2016-09-25 12:42:08.0000000 |3 |11,85
3160036 |2016-09-30 11:30:52.0000000 |2016-09-25 12:42:08.0000000 |5 |11,83
3160039 |2016-09-27 08:34:59.0000000 |NULL |0 |47,17
3160039 |2016-09-27 09:05:52.0000000 |2016-09-27 08:34:59.0000000 |0 |53,08
3160039 |2016-09-27 09:12:46.0000000 |2016-09-27 08:34:59.0000000 |0 |52,99
3160039 |2016-09-27 09:19:32.0000000 |2016-09-27 08:34:59.0000000 |0 |52,73
3160039 |2016-09-27 09:29:42.0000000 |2016-09-27 08:34:59.0000000 |0 |53,59
3160039 |2016-09-28 08:28:50.0000000 |2016-09-27 08:34:59.0000000 |1 |18,4
3160051 |2016-10-24 05:57:18.0000000 |NULL |0 |11,13
3160051 |2016-10-24 06:12:35.0000000 |2016-10-24 05:57:18.0000000 |0 |11,18
3160051 |2016-10-27 12:42:58.0000000 |2016-10-24 05:57:18.0000000 |3 |11,17
3160051 |2016-10-27 12:56:33.0000000 |2016-10-24 05:57:18.0000000 |3 |11,31
使用Partition by ...的类似解决方案
SELECT a.* , datediff(day,LastDate,a.[DateTime]) as Diff
FROM (
SELECT
T0.[Batch],
T0.[DateTime],
MIN(T0.[DateTime]) OVER (PARTITION BY T0.[Batch]) as LastDate,
T0.[PG] AS [PG]
FROM t005 as T0
) as a
ORDER BY a.[Batch],
a.[DateTime]