+ -
当前位置:首页 → 问答吧 → 求一难受的SQL语句

求一难受的SQL语句

时间:2011-12-13

来源:互联网

表A
ID ADD_TIME SL
1 2011-09-22 11:23:52 2
2 2011-09-24 11:36:52 4
3 2011-09-24 14:21:52 2
4 2011-09-24 15:25:52 3
5 2011-09-24 15:06:52 3
6 2011-09-24 19:36:52 1
7 2011-09-24 20:36:52 7
8 2011-09-24 22:36:52 11
9 2011-09-26 20:36:52 12

如上,如何取得2011-09-24号,每个小时时段的SL总和?得到如下

11 4
14 2
15 6
19 1
20 7
22 11

作者: yejian520   发布时间: 2011-12-13

SQL code

select datepart(hh,add_time) as hh,sum(sl) as sl
from a
where convert(varchar(8),add_time,112) = '20110924'
group by datepart(hh,add_time)

作者: AcHerat   发布时间: 2011-12-13

SQL code

select datepart(hh,add_time) as hh,sum(sl) as sl
from a
where add_time like '2011-09-24%'
group by datepart(hh,add_time)

作者: sjcss   发布时间: 2011-12-13

SQL code
select datepart(hh,add_time) as hh,sum(sl) as sl
from a
where convert(varchar(8),add_time,112) = '20110924'
group by datepart(hh,add_time)
order by datepart(hh,add_time)

作者: kingtiy   发布时间: 2011-12-13