【转载】sql 行专列经典

低效
 select host,max(h0), max(h1), max(h2), max(h3), max(h4) from (select 
tmp.host, 
(case tmp.hour when '00' then  if(tmp.count is null,0,1)  end) h0,
(case tmp.hour when '01' then tmp.count end) h1,
(case tmp.hour when '02' then tmp.count end) h2,
(case tmp.hour when '03' then tmp.count end) h3,
(case tmp.hour when '04' then tmp.count end) h4
from (select host,substr(ymdhms,9,2) as hour,
       count(*) as count 
      from cndnslogsec where dt = '20131220' and node='njtel' and host ='t.cn' and ymdhms is not null 
      group by host,substr(ymdhms,9,2) 
      ) tmp limit 10)  table1 group by host; 


高效:
  select host, 
count(*) as h,
sum (case substr(ymdhms,9,2)  when '00' then  1 else 0 end) as h0,
sum (case substr(ymdhms,9,2)  when '01' then  1 else 0 end) as h1, 
sum (case substr(ymdhms,9,2)  when '02' then  1 else 0 end) as h2,
sum (case substr(ymdhms,9,2)  when '03' then  1 else 0 end) as h3,
sum (case substr(ymdhms,9,2)  when '04' then  1 else 0 end) as h4,
sum (case substr(ymdhms,9,2)  when '05' then  1 else 0 end) as h5,
sum (case substr(ymdhms,9,2)  when '06' then  1 else 0 end) as h6,
sum (case substr(ymdhms,9,2)  when '07' then  1 else 0 end) as h7,
sum (case substr(ymdhms,9,2)  when '08' then  1 else 0 end) as h8,
sum (case substr(ymdhms,9,2)  when '09' then  1 else 0 end) as h9,
sum (case substr(ymdhms,9,2)  when '10' then  1 else 0 end) as h10,
sum (case substr(ymdhms,9,2)  when '11' then  1 else 0 end) as h11,
sum (case substr(ymdhms,9,2)  when '12' then  1 else 0 end) as h12,
sum (case substr(ymdhms,9,2)  when '13' then  1 else 0 end) as h13,
sum (case substr(ymdhms,9,2)  when '14' then  1 else 0 end) as h14,
sum (case substr(ymdhms,9,2)  when '15' then  1 else 0 end) as h15,
sum (case substr(ymdhms,9,2)  when '16' then  1 else 0 end) as h16,
sum (case substr(ymdhms,9,2)  when '17' then  1 else 0 end) as h17,
sum (case substr(ymdhms,9,2)  when '18' then  1 else 0 end) as h18,
sum (case substr(ymdhms,9,2)  when '19' then  1 else 0 end) as h19,
sum (case substr(ymdhms,9,2)  when '20' then  1 else 0 end) as h20,
sum (case substr(ymdhms,9,2)  when '21' then  1 else 0 end) as h21,
sum (case substr(ymdhms,9,2)  when '22' then  1 else 0 end) as h22,
sum (case substr(ymdhms,9,2)  when '23' then  1 else 0 end) as h23
from cndnslogsec where dt = '20131220' and node='njtel'  and ymdhms is not null 
group by host
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值