【发布时间】:2020-01-06 22:10:32
【问题描述】:
我有一个非常大的时间数据集,显示了整个美国在 2 年内的停电情况。我想使用来自 4 列的不同记录来计算中断的持续时间。 CustomersOut 的“0”值被视为重置。
例子:
Utility1, State1, County1, City1, 1500, 0, 2017-12-28 12:25:00
Utility1, State1, County1, City1, 1500, 50, 2018-01-01 03:25:00
Utility1, State1, County1, City1, 1500, 65, 2018-01-01 03:36:00
Utility1, State1, County1, City1, 1500, 40, 2018-01-01 03:55:00
Utility1, State1, County1, City1, 1500, 0, 2018-01-01 04:34:00
这意味着在 City1 中,有 50 位顾客外出 11 分钟、65 位顾客外出 19 分钟、40 名顾客外出 39 分钟,总共有 55.75 个顾客外出时间。
期望的结果:
Utility1, State1, County1, City1, 1500, 0, 2017-12-28 12:25:00, NULL
Utility1, State1, County1, City1, 1500, 50, 2018-01-01 03:25:00, 00:11.00
Utility1, State1, County1, City1, 1500, 65, 2018-01-01 03:36:00, 00:19.00
Utility1, State1, County1, City1, 1500, 40, 2018-01-01 03:55:00, 00:39.00
Utility1, State1, County1, City1, 1500, 0, 2018-01-01 04:34:00, NULL
【问题讨论】:
-
"TIMESTAMP" 是一种已弃用的数据类型,与日期或时间无关。我想你的意思是在这里说“日期时间”。了解这种区别很重要。
-
请显示您想要的结果。
-
你是绝对正确的。谢谢指正
-
@GordonLinoff 感谢您的评论。帖子已更新
标签: sql sql-server tsql date group-by