group by 之前如何取count(id) 为零的记录
本帖最后由 jianming2032 于 2013-06-08 15:34:14 编辑
要求是取当天的各个时间段的记录数,具体查询语句为
select count(id) as TRANSCOUNT,CASE WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '00:00:00' AND '00:59:59' THEN 0
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '01:00:00' AND '01:59:59' THEN 1
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '02:00:00' AND '02:59:59' THEN 2
……
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '22:00:00' AND '22:59:59' THEN 22
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '23:00:00' AND '23:59:59' THEN 23
END TIME_NUM from t_transactioninfo where 1=1 and trunc(sysdate)=trunc(HELPTIME)
GROUP BY CASE WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '00:00:00' AND '00:59:59' THEN 0
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '01:00:00' AND '01:59:59' THEN 1
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '02:00:00' AND '02:59:59' THEN 2
……
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '22:00:00' AND '22:59:59' THEN 22
WHEN TO_CHAR(HELPTIME, 'HH24:mi:ss') BETWEEN '23:00:00' AND '23:59:59' THEN 23
END
结果出来的数据是
请问如何把所有时间段的记录都显示出来默认为0
------解决方案--------------------多半有条件是为0 的显示不出来,你仔细看看
------解决方案--------------------你放弃group BY
改用order by
你不是想显示所有的数据嘛 你既然已经把时间一段一段的都 换成数字了 那么 order by 就可以了