我的统计表TJ里有如下统计数据:
(单位序号 缺陷分类编号      汇总数目)dwxh    qxflbh     hzsm
2 a     9
2 b    13
2 c     6
6 a     1
6 b     5
6 c     3
我想产生一个这样的查询:
单位序号 缺陷编号a汇总数目 缺陷编号b汇总数目 缺陷编号c汇总数目 
2 9 13 6
6 1 5 3sql应该怎么写啊.100分送上.

解决方案 »

  1.   

    select dwxh,max(decode(qxflbh,'a',hzsm)) a_hzsm,
           max(decode(qxflbh,'b',hzsm)) b_hzsm,
           max(decode(qxflbh,'c',hzsm)) c_hzsm
    from tj
    group by dwxh
      

  2.   

    select 
    dwxh 单位序号
    ,sum(decode(qxflbh,'a',hzsm,0)) 缺陷编号a汇总数目
    ,sum(decode(qxflbh,'b',hzsm,0)) 缺陷编号b汇总数目
    ,sum(decode(qxflbh,'c',hzsm,0)) 缺陷编号c汇总数目
    from test group by dwxh
    /select 
    dwxh 单位序号
    ,sum(case when qxflbh = 'a' then hzsm else 0 end) 缺陷编号a汇总数目
    ,sum(case when qxflbh = 'b' then hzsm else 0 end) 缺陷编号b汇总数目
    ,sum(case when qxflbh = 'c' then hzsm else 0 end) 缺陷编号c汇总数目
    from test group by dwxh
    /
      

  3.   

    SQL> select dwxh,
      2  max(decode(qxflbh,'a',hzsm)) a,
      3  max(decode(qxflbh,'b',hzsm)) b,
      4  max(decode(qxflbh,'c',hzsm)) c
      5  from tj
      6  group by dwxh;      DWXH          A          B          C
    ---------- ---------- ---------- ----------
             2          9         13          6
             6          1          5          3已选择2行。