现有表如下:idWATCH_TIMEONLINE_COUNT12012-3-716:40:42122012-3-717:48:55232012-3-718:48:071编写如下sql代码:SELECT(casewhenSUBSTR(WATCH_TIME,12,8)>='16:00:00'andSUBSTR(WAT...
现有表如下:
id WATCH_TIME ONLINE_COUNT
1 2012-3-7 16:40:42 1
2 2012-3-7 17:48:55 2
3 2012-3-7 18:48:07 1
编写如下sql代码:
SELECT (case when SUBSTR(WATCH_TIME,12,8)>='16:00:00' and SUBSTR(WATCH_TIME,12,8)<='16:59:59' then ONLINE_COUNT else 0 end) as h16,
(case when SUBSTR(WATCH_TIME,12,8)>='17:00:00' and SUBSTR(WATCH_TIME,12,8)<='17:59:59' then ONLINE_COUNT else 0 end)as h17,
(case when SUBSTR(WATCH_TIME,12,8)>='18:00:00' and SUBSTR(WATCH_TIME,12,8)<='18:59:59' then ONLINE_COUNT else 0 end)as h18,
(case when SUBSTR(WATCH_TIME,12,8)>='19:00:00' and SUBSTR(WATCH_TIME,12,8)<='19:59:59' then ONLINE_COUNT else 0 end)as h19
FROM `t_log_online_count` where date(WATCH_TIME)=date('2012-03-07');
获取结果如下:
h16 h17 h18 h19
1 0 0 0
0 2 0 0
0 0 1 0
期望获取的结果如下:
h16 h17 h18 h19
1 2 1 0
展开