我
有一个公式可以根据特定条件计算日期之间的差异(比较当前行和下一行的值。
数据如下所示:
AppID Status Created Date
281 Default 1/6/2015
281 Withdrawn By Candidate 1/7/2015
301 Default 1/8/2015
....
公式以这种方式比较应用程序ID: Excel formula 到目前为止,我想出了这个选择,它比较当前行和下一行的值。
SELECT CASE
WHEN AppID = LEAD(AppID) OVER (ORDER BY AppID)
THEN DATEDIFF(DAY, [Created Date], LEAD([Created Date]) OVER (ORDER BY AppID))
ELSE DATEDIFF(DAY, [Created Date], GETDATE())
END as Days, *
FROM Application
它显示了正确的结果。但是当我尝试使用更新时:
UPDATE Application_Days
SET [Days] = (SELECT
CASE
WHEN AppID = LEAD(AppID) OVER (Partition by AppId ORDER BY AppID)
THEN DATEDIFF(DAY, [Created Date], LEAD([Created Date]) OVER (Partition by AppId ORDER BY AppID))
ELSE DATEDIFF(DAY, [Created Date], GETDATE())
END
)
似乎CASE结果总是错误的。
有什么建议吗? 最好的问候,谢谢你的时间!
编辑 - 结果表如下所示:
AppID Status Created Date Days
281 Default 01/06/2015 1
281 Withdrawn 01/07/2015 1157
301 Default 01/08/2015 11
301 CV Review 01/19/2015 37
301 DQ CV Revi 02/25/2015 1108
321 Default 01/07/2015 12
321 CV Review 01/19/2015 29
321 DQ CV Revi 02/17/2015 1116
341 Default 01/07/2015 0
341 CV Review 01/07/2015 12
341 Shortlist 01/19/2015 0
341 CV Review 01/19/2015 37
341 DQ CV Revi 02/25/2015 1108
362 Default 01/07/2015 1157
365 Default 01/02/2015
我得到了这个:
AppID Status Created Date Days
281 Default 2015-01-06 1158
281 Withdrawn By C 2015-01-07 1157
301 Default 2015-01-08 1156
301 CV Review 2015-01-19 1145
301 DQ CV Review 2015-02-25 1108
321 Default 2015-01-07 1157
321 CV Review 2015-01-19 1145
321 DQ CV Review 2015-02-17 1116
341 Default 2015-01-07 1157
341 CV Review 2015-01-07 1157
341 Shortlist 2015-01-19 1145
341 CV Review 2015-01-19 1145
341 DQ CV Review 2015-02-25 1108
362 Default 2015-01-07 1157
365 Default 01/02/2015