Sql 使用不同的记录从DATETIME计算并创建持续时间
我有一个非常大的时间数据集,显示了整个美国2年内的停电情况。我想使用来自4列的不同记录来计算停机的持续时间。CustomerOut的“0”值被视为重置 例如:Sql 使用不同的记录从DATETIME计算并创建持续时间,sql,sql-server,tsql,date,group-by,Sql,Sql Server,Tsql,Date,Group By,我有一个非常大的时间数据集,显示了整个美国2年内的停电情况。我想使用来自4列的不同记录来计算停机的持续时间。CustomerOut的“0”值被视为重置 例如: 2017-12-28 12:25:00 2018年01月01日03:25:00 2018年01月01日03:36:00 2018年01月01日03:55:00 2018年1月0日1500:01-01 04:34:00 这意味着在1号城市,有50名顾客外出11分钟,65名顾客外出19分钟,40名顾客外出39分钟,共计55.75个顾客小时
2017-12-28 12:25:00
2018年01月01日03:25:00
2018年01月01日03:36:00
2018年01月01日03:55:00
2018年1月0日1500:01-01 04:34:00
这意味着在1号城市,有50名顾客外出11分钟,65名顾客外出19分钟,40名顾客外出39分钟,共计55.75个顾客小时
预期结果:
Utility1,State1,County1,City1,15002017-12-2812:25:00,空
1号城市1号县1号州1号公用设施,邮编:1500,50,2018-01-01 03:25:00,00:11.00
2018年01月01日03:36:00,00:19.00
公用设施1,州1,县1,城市1,1500,40,2018-01-01 03:55:00,00:39.00
公用设施1,国家1,国家1,城市1,1500,0,2018-01-01 04:34:00,空
您可以从一个子查询开始,该子查询计算当前记录与下一记录之间的差异。然后,外部查询筛选与断电和聚合对应的记录:
select
UtilityName,
StateName,
CountyName,
CityName,
sum(CustomersOut) CustomersOut,
sum(MinutesOut) MinutesOut,
sum(CustomersOut * MinutesOut) / 60.0 CustomersHoursOut
from (
select
UtilityName,
StateName,
CountyName,
CityName,
CustomersOut,
datediff(
minute,
RecordedDateTime,
lead(RecordedDateTime) over(
partition by UtilityName, StateName, CountyName, CityName
order by RecordedDateTime
)
) MinutesOut
from mytable
) t
where CustomersOut > 0
group by
UtilityName,
StateName,
CountyName,
CityName
:
UtilityName | StateName | CountyName | CityName | CustomersOut | MinutesOut | CustomersHoursOut
:---------- | :-------- | :--------- | :------- | -----------: | ---------: | :------------------
Utility1 | State1 | County1 | City1 | 155 | 69 | 55.750000
UtilityName | StateName | CountyName | CityName | CustomersTracked | CustomersOut | RecordedDateTime | MinutesOut
:---------- | :-------- | :--------- | :------- | ---------------: | -----------: | :------------------ | ---------:
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 | 11
Utility1 | State1 | County1 | City1 | 1500 | 65 | 2018-01-01 03:36:00 | 19
Utility1 | State1 | County1 | City1 | 1500 | 40 | 2018-01-01 03:55:00 | 39
Utility1 | State1 | County1 | City1 | 1500 | 0 | 2018-01-01 04:34:00 | null
:
UtilityName | StateName | CountyName | CityName | CustomersOut | MinutesOut | CustomersHoursOut
:---------- | :-------- | :--------- | :------- | -----------: | ---------: | :------------------
Utility1 | State1 | County1 | City1 | 155 | 69 | 55.750000
UtilityName | StateName | CountyName | CityName | CustomersTracked | CustomersOut | RecordedDateTime | MinutesOut
:---------- | :-------- | :--------- | :------- | ---------------: | -----------: | :------------------ | ---------:
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 | 11
Utility1 | State1 | County1 | City1 | 1500 | 65 | 2018-01-01 03:36:00 | 19
Utility1 | State1 | County1 | City1 | 1500 | 40 | 2018-01-01 03:55:00 | 39
Utility1 | State1 | County1 | City1 | 1500 | 0 | 2018-01-01 04:34:00 | null
实用程序名称| StateName | CountyName | CityName | CustomersTracked | CustomersOut | RecordedDateTime |分钟
:---------- | :-------- | :--------- | :------- | ---------------: | -----------: | :------------------ | ---------:
公用设施1国家1国家1城市1500 0 2017-12-28 12:25:00空
公用设施1州1县1市1500 50 2018-01-01 03:25:00 11
公用设施1州1县1市1500 65 2018-01-01 03:36:00 19
公用设施1州1县1市1500 40 2018-01-01 03:55:00 39
实用性1国家1国家1城市1500 0 2018-01-01 04:34:00零
“TIMESTAMP”是一种不推荐使用的数据类型,与日期或时间无关。我想你的意思是说“约会时间”。理解这种区别很重要。请展示你想要的结果。你是绝对正确的。谢谢你的邀请correction@GordonLinoff谢谢你的评论。帖子已经更新。既然OP已经更新了问题,您只需删除
分组依据
。