Sql server 按houly分组数据
我有三列,Sql server 按houly分组数据,sql-server,tsql,group-by,aggregate,grouping,Sql Server,Tsql,Group By,Aggregate,Grouping,我有三列,日期,时间,和生产总额。我想把它们分组,得到每个按日期过滤的小时间隔的总和,以及每个按日期汇总的小时间隔的平均值 样本数据 Date Time Production total 11/30/2016 7:29 5 11/30/2016 7:35 6 11/30/2016 8:05 5 11/30/2016 8:11 5 12/1/2016 0:04 5 12/1/2016 0:10 6 12/1/2016 1:
日期
,时间
,和生产总额
。我想把它们分组,得到每个按日期过滤的小时间隔的总和,以及每个按日期汇总的小时间隔的平均值
样本数据
Date Time Production total
11/30/2016 7:29 5
11/30/2016 7:35 6
11/30/2016 8:05 5
11/30/2016 8:11 5
12/1/2016 0:04 5
12/1/2016 0:10 6
12/1/2016 1:04 6
12/1/2016 1:10 5
12/2/2016 6:52 5
12/2/2016 6:58 4
12/2/2016 7:04 5
12/2/2016 7:10 5
我希望结果是这样的
Date Time Production total
11/30/2016 7:00 11
11/30/2016 8:00 10
12/1/2016 0:00 11
12/1/2016 1:00 11
12/2/2016 6:00 9
12/2/2016 7:00 10
您可以使用DatePart获取小时数,并使用group by求和
select [date], datepart(hour,[time]), sum([production total]) from #yourtable
group by [date], datepart(hour, [time])
附表内容:
create table #yourtable ( Date date, time time, [Production Total] int)
insert into #yourtable (
[Date], [Time] ,[Production total] ) values
('11/30/2016' ,'7:29', 5 )
,('11/30/2016' ,'7:35', 6 )
,('11/30/2016' ,'8:05', 5 )
,('11/30/2016' ,'8:11', 5 )
,('12/1/2016' ,'0:04', 5 )
,('12/1/2016' ,'0:10', 6 )
,('12/1/2016' ,'1:04', 6 )
,('12/1/2016' ,'1:10', 5 )
,('12/2/2016' ,'6:52', 5 )
,('12/2/2016' ,'6:58', 4 )
,('12/2/2016' ,'7:04', 5 )
,('12/2/2016' ,'7:10', 5 )
到目前为止你试过什么
DATEPART()
和groupby
是你的朋友,但我不知道从哪里开始,因为groupby不起作用..发布你已经开始的内容。Define不起作用。请分享您的信息,以帮助我们帮助您。