gpt4 book ai didi

sql-server-2005 - 我如何聚合/旋转这些数据?

转载 作者:行者123 更新时间:2023-12-01 04:15:22 25 4
gpt4 key购买 nike

我有一组记录来跟踪系统可用性。样本数据:

System_ID   Event    DateOfEvent
1 Down 2010-05-01 13:20:10
1 Up 2010-05-01 13:25:19
1 Down 2010-05-05 10:12:12
1 Up 2010-05-06 14:10:16
2 Down 2010-05-05 20:22:22
2 Up 2010-05-06 23:20:26

如何将此数据转换为以下布局?
System_Id   DowntimeStart         DowntimeInSeconds
1 2010-05-01 13:20:10 309
1 2010-05-05 10:12:12 100684
2 2010-05-05 20:22:22 97084

最佳答案

你可以试试这个:

Select et1.System_ID, et1.[Date] as DateDown, et2.[Date] as DateUp, DATEDIFF(s, et1.[Date], et2.[Date]) as DownForSeconds
From EventTable et1
Left Join EventTable et2 On et1.System_ID = et2.System_ID and et2.[Event] = 'Up'
Where
et1.[Event] = 'Down'
and et2.[Date] = (Select Top 1 [Date] From EventTable Where System_ID = et2.System_ID and [Date] > et1.[Date])

关于sql-server-2005 - 我如何聚合/旋转这些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4040991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com