我想显示第2阶段最新记录的减法和第3阶段的最新记录 这是表结构:
------------------------------------
name |stage| date
------------------------------------
| ABC | 2 | 2016-5-07
| ABC | 2 | 2016-6-08
| ABC | 3 | 2016-6-07
| ABC | 3 | 2016-12-08
| PQR | 2 | 2016-6-07
| PQR | 2 | 2016-8-12
| PQR | 3 | 2016-9-07
| PQR | 3 | 2016-10-08
我想从2016-12-08日期减去日期2016-6-08,其中名称是ABC,它们将是183天 从2016-10-08减去2016-8-12,名称为PQR
输出应该像
一样| name | Date |
---------------
| ABC | 183 |
| PQR | 61 |
答案 0 :(得分:1)
WITH cte AS (
SELECT t.[name], t.[stage], t.[date]
FROM
(
SELECT [name], [stage], [date],
ROW_NUMBER() OVER (PARTITION BY [name], [stage] ORDER BY [date] DESC) rn
) t
WHERE t.rn = 1
)
SELECT [name], DATEDIFF(day, MIN(t.[date]), MAX(t.[date])) AS [Date]
FROM cte t
GROUP BY [name]
答案 1 :(得分:1)
SELECT [NAME], DATEDIFF(DAY, MIN(x.[DATE]), MAX(x.[DATE])) AS [DATE]
FROM
(SELECT T.[NAME], T.[STAGE], T.[DATE]
FROM
(
SELECT [NAME], [STAGE], [DATE],
ROW_NUMBER() OVER (PARTITION BY [NAME], [STAGE] ORDER BY [DATE] DESC) RN FROM #TABLE3
) T
WHERE T.RN = 1
)X
GROUP BY [NAME]
或
SELECT NAME, DATEDIFF(DD,MIN([DATE]),MAX([DATE])) AS [DATE] FROM
(SELECT NAME, STAGE, MAX([DATE]) AS [DATE] FROM #TABLE3 GROUP BY NAME, STAGE) C
GROUP BY NAME;
答案 2 :(得分:1)
with cte as (
select name, stage, max([date]) as [date] from #YourTable group by name, stage
)
select name, datediff(dd,min([date]),max([date])) as [Date] from cte group by name;
答案 3 :(得分:1)
其他答案是减去忽略阶段的最小和最大日期,而不是每个阶段的最大日期。试试这个:
SELECT s2.name, DATEDIFF(day, MAX(s2.date), MAX(s3.date)) days
FROM MyTable s2 JOIN MyTable s3 ON s2.name = s3.name
WHERE s2.stage = 2 AND s3.stage = 3
GROUP BY s2.name;
JOIN
将表的两个副本组合成一个大表,该表对原始表中具有相同名称的两行的每个组合都有一行。
name | s2.stage | s2.date | s3.stage | s3.date
------+----------+------------+----------+------------
ABC | 2 | 2016-06-08 | 2 | 2016-06-08
ABC | 2 | 2016-06-08 | 3 | 2016-06-07
ABC | 2 | 2016-06-08 | 3 | 2016-12-08
ABC | 2 | 2016-06-08 | 2 | 2016-05-07
ABC | 3 | 2016-06-07 | 2 | 2016-06-08
ABC | 3 | 2016-06-07 | 3 | 2016-06-07
ABC | 3 | 2016-06-07 | 3 | 2016-12-08
ABC | 3 | 2016-06-07 | 2 | 2016-05-07
ABC | 3 | 2016-12-08 | 2 | 2016-06-08
ABC | 3 | 2016-12-08 | 3 | 2016-06-07
ABC | 3 | 2016-12-08 | 3 | 2016-12-08
ABC | 3 | 2016-12-08 | 2 | 2016-05-07
ABC | 2 | 2016-05-07 | 2 | 2016-06-08
ABC | 2 | 2016-05-07 | 3 | 2016-06-07
ABC | 2 | 2016-05-07 | 3 | 2016-12-08
ABC | 2 | 2016-05-07 | 2 | 2016-05-07
PQR | 2 | 2016-08-12 | 2 | 2016-08-12
PQR | 2 | 2016-08-12 | 3 | 2016-09-07
PQR | 2 | 2016-08-12 | 3 | 2016-10-08
PQR | 2 | 2016-08-12 | 2 | 2016-06-07
PQR | 3 | 2016-09-07 | 2 | 2016-08-12
PQR | 3 | 2016-09-07 | 3 | 2016-09-07
PQR | 3 | 2016-09-07 | 3 | 2016-10-08
PQR | 3 | 2016-09-07 | 2 | 2016-06-07
PQR | 3 | 2016-10-08 | 2 | 2016-08-12
PQR | 3 | 2016-10-08 | 3 | 2016-09-07
PQR | 3 | 2016-10-08 | 3 | 2016-10-08
PQR | 3 | 2016-10-08 | 2 | 2016-06-07
PQR | 2 | 2016-06-07 | 2 | 2016-08-12
PQR | 2 | 2016-06-07 | 3 | 2016-09-07
PQR | 2 | 2016-06-07 | 3 | 2016-10-08
PQR | 2 | 2016-06-07 | 2 | 2016-06-07
WHERE s2.stage=2 AND s3.stage=3
将其限制为s2列对应于阶段2且s3列对应于阶段3的行:
name | s2.stage | s2.date | s3.stage | s3.date
------+----------+------------+----------+------------
ABC | 2 | 2016-05-07 | 3 | 2016-06-07
ABC | 2 | 2016-05-07 | 3 | 2016-12-08
ABC | 2 | 2016-06-08 | 3 | 2016-06-07
ABC | 2 | 2016-06-08 | 3 | 2016-12-08
PQR | 2 | 2016-06-07 | 3 | 2016-09-07
PQR | 2 | 2016-06-07 | 3 | 2016-10-08
PQR | 2 | 2016-08-12 | 3 | 2016-09-07
PQR | 2 | 2016-08-12 | 3 | 2016-10-08
然后,我们使用MAX(...)
函数和GROUP BY name
子句为每个名称取每个日期字段的最大值,并使用DATEDIFF
函数减去它们。
请注意,您应该使用您正在使用的特定数据库标记问题;上述SQL在Microsoft SQL Server中有效,但其他数据库的日期差异不同,并且支持JOIN
... USING
语法,可以略微简化查询。例如,这就是Postgres的样子:
SELECT name, MAX(s3.date) - MAX(s2.date) days
FROM MyTable s2 JOIN MyTable s3 USING (name)
WHERE s2.stage = 2 AND s3.stage = 3
GROUP BY name;