Table one:
 ID      TYPE       COUNT
TOM       100         2
TOM       200         3
TOM       300         1
JOHN      200         3
JOHN      300         8
Want:
 ID      100       200       300
TOM       2         3         1
JOHN                3         8

解决方案 »

  1.   

    在标题里面按了下回车,结果就发出去了。而且自己发的帖子没有权限修改。郁闷。
    补充一下
    Table one:
    ID      TYPE      COUNT
    TOM      100        2
    TOM      200        3
    TOM      300        1
    JOHN      200        3
    JOHN      300        8通过上表,用SQL得到下述结果。怎么写?Want:
    ID      100      200      300
    TOM      2        3        1
    JOHN              3        8
      

  2.   

    select distinct id,
      max(case type when 100 then count else 0 end),
      max(case type when 200 then count else 0 end),
      max(case type when 300 then count else 0 end)
    from want 
    group by id
      

  3.   


    select distinct id, 
      max(case type when 100 then count else 0 end) 100, 
      max(case type when 200 then count else 0 end) 200, 
      max(case type when 300 then count else 0 end) 300
    from want 
    group by id或是用decodeselect distinct id, 
      max(decode(type,100,count,0)) 100, 
      max(decode(type,200,count,0)) 200, 
      max(decode(type,300,count,0)) 300
    from want 
    group by id