我需要找到SQL Server 2014中状态更改之间的天数。
例如,请参阅下面的数据
+--------+--------+------------+-------------+
| status | Number | updated_on | opened_at |
+--------+--------+------------+-------------+
| Draft | 100 | 2017-11-03 | 2017-11-03 |
| Draft | 100 | 2017-12-12 | 2017-11-03 |
| WIP | 100 | 2017-12-12 | 2017-11-03 |
| Appr | 100 | 2018-01-05 | 2017-11-03 |
| Launch | 100 | 2018-01-10 | 2017-11-03 |
| Close | 100 | 2018-01-11 | 2017-11-03 |
+--------+--------+------------+-------------+
根据以上输入,我需要
Draft --- 40 days,
WIP --- 23 days,
appro -- 5 days,
deploy/launch - 1 days,
closed --- 69 days
请帮我查询SQL查询结果。
感谢。
答案 0 :(得分:1)
我认为你的数字是对的。但这应该做你想要的,假设状态是唯一的:
select status,
datediff(day, updated_on, lead(updated_on) over (order by updated_on) ) as days
from t;
但是,我不了解第一个和最后一个数字。
答案 1 :(得分:0)
试试这个
SELECT
tb.status,
DATEDIFF(dayofyear, tb.opened_at, tb.LastUpdate) AS DaysInDifference
FROM
(
SELECT
DISTINCT
status,
Max(updated_on) OVER(PARTITION BY [status] )LastUpdate,
opened_at
FROM Table1
)AS tb