select name,
SUM(case ab when '1' then 1 else 0 end)as a,
SUM(case ab when '2' then 1 else 0 end)as b,
SUM(case ab when '3' then 1 else 0 end)as c,
SUM(case ab when '4' then 1 else 0 end)as d,
SUM(case ab when '5' then 1 else 0 end)as e,
SUM(case ab when '6' then 1 else 0 end)as f,
SUM(case ab when '7' then 1 else 0 end)as g,
SUM(case ab when '8' then 1 else 0 end)as h,
SUM(case ab when '9' then 1 else 0 end)as i,
SUM(case ab when '20' then 1 else 0 end)as z,
SUM(case ab when '21' then 1 else 0 end)as k,
SUM(case ab when '22' then 1 else 0 end)as l,
SUM(case ab when '23' then 1 else 0 end)as m,
SUM(case ab when '24' then 1 else 0 end)as n,
SUM(case ab when '30' then 1 else 0 end)as z,
SUM(case ab when '31' then 1 else 0 end)as k,
SUM(case ab when '32' then 1 else 0 end)as l,
SUM(case ab when '33' then 1 else 0 end)as m,
SUM(case ab when '34' then 1 else 0 end)as n,

解决方案 »

  1.   

    SUM(case ab when '35' then 1 else 0 end)as o,from tb group by name
    请帮忙简化
      

  2.   

    查询会返回name,和各个ab不同取值的数据行数。
    你的查询列表本来就需要这些列,没有必要简化。
    SUM(decode ( ab ,'34' ,1 ,0 ))
    如果说把case换成decode,也谈不上简化吧
      

  3.   

    老师我就是不写那么多SUM(case ab when '&&' then 1 else 0 end)as &、我就是想有没有什么办法不写那么多,有没有循环多办法啊?
      

  4.   

    你觉得有行列转换的必要吗》直接gourp by不就成了
      

  5.   


    他现在已经行列转换了,把列转成行A,B,C,D了.