UPDATE: Кроме того, фильтр пропускал + убывание сортировки ...
Вы, возможно, пропустили связь между партиями?
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
Аналогичное решение с использованием раздела по ..
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]
Ваш текущий код дает вам количество дней в партии с момента запуска этой партии до последнего времени запуска в целом. Что вы хотите, чтобы это по-другому? – Ben
Нет, это не так. Если вы посмотрите на результаты выборки, партия 3160039 началась с 27-го. Он должен сказать 0 дней для первой записи, но он говорит 2 дня, так как была еще одна партия (3160036), которая началась 25-го числа. Конечная запись для партии 3160051 должна сказать 3 дня, но она говорит 32. – coblenski