数据库 case when then

select d.rq,d.sjd,count(d.num) as total from (select c.rq,c.sj,
case 
WHEN '06:01'<=c.sj and c.sj<='06:30' THEN '06:00'
WHEN '06:31'<=c.sj and c.sj<='07:00' THEN '07:00'
WHEN '07:01'<=c.sj and c.sj<='07:30' THEN '08:00'
WHEN '07:31'<=c.sj and c.sj<='08:00' THEN '09:00'
WHEN '08:01'<=c.sj and c.sj<='08:30' THEN '10:00'
WHEN '08:31'<=c.sj and c.sj<='12:00' THEN '12:00'

WHEN '12:01'<=c.sj and c.sj<='12:30' THEN '12:00'
WHEN '12:31'<=c.sj and c.sj<='13:00' THEN '13:00'
WHEN '13:01'<=c.sj and c.sj<='13:30' THEN '13:00'
WHEN '13:31'<=c.sj and c.sj<='14:00' THEN '14:00'

WHEN '14:01'<=c.sj and c.sj<='17:00' THEN '14:00'
WHEN '17:01'<=c.sj and c.sj<='17:30' THEN '17:00'
WHEN '17:31'<=c.sj and c.sj<='18:00' THEN '18:00'
WHEN '18:01'<=c.sj and c.sj<='18:30' THEN '18:00'
WHEN '18:31'<=c.sj and c.sj<='19:00' THEN '19:00'

WHEN '19:01'<=c.sj and c.sj<='19:30' THEN '19:00'
WHEN '19:31'<=c.sj and c.sj<='20:00' THEN '20:00'
WHEN '20:01'<=c.sj and c.sj<='20:30' THEN '20:00'
WHEN '20:31'<=c.sj and c.sj<='21:00' THEN '21:00'
end as sjd,
  1 as num
from 
(select DATE_FORMAT(t.jyrq,'%Y-%m-%d') as rq ,DATE_FORMAT(t.jyrq,'%H:%i') sj from t_ykt_grjy t where t.jylx='210' and left(t.JYRQ,10) ='2015-09-12' 
)c 

)d GROUP BY d.rq,d.sjd


================================================

select d.xbdm,d.rq,d.sjd,d.xq,sum(num)rs from
(select c.xbdm,c.rq,c.sj,case c.sj
when 08 then '9:00'
when 09 then '10:00'
when 10 then '11:00'
when 11 then '12:00'
when 12 then '13:00'
when 13 then '14:00'
when 14 then '15:00'
when 15 then '16:00'
when 16 then '17:00'
when 17 then '18:00'
when 18 then '19:00'
when 19 then '20:00'
when 20 then '21:00'
when 21 then '22:00'
else '22:00以后'
end  as sjd,
case c.dw
when 1 then '星期日'
when 2 then '星期一'
when 3 then '星期二'
when 4 then '星期三'
when 5 then '星期四'
when 6 then '星期五'
when 7 then '星期六'
end  as xq,
1 num
from
(select a.xbdm,date_format(b.jysj,'%Y-%m-%d')rq,date_format(b.jysj,'%H')sj,dayofweek(b.jysj)dw
from 
t_stu_basic a,t_ykt_grjy b where a.xh = b.xh 
and b.jylx = '210'
and b.jysj > '2015-08-11 00:00:00'
)c 
)d group by d.xbdm,d.rq,d.sjd,d.xq 

猜你喜欢

转载自blog.csdn.net/hfuningn/article/details/78676753
今日推荐