表 table1
id       name      type    hours
100     james      正常     2
100     james      周末     3
100     james      假日     1
101     jack       正常   2
101     jack       周末   3
如何分組變成
id     name   正常    周末      假日   total
100   james    2        3        1       6 
101    jack    2        3                5

解决方案 »

  1.   

    select id, name,max(decode(type,'正常',hours)) col1,
                    max(decode(type,'周未',hours)) col2,
    max(decode(type,'假日',hours)) col3,
    sum(hours) from combiancol group by id,name
      

  2.   

    希望你的Type是固定的.  若是多变的. 就麻烦了. 搞不定了.
      

  3.   

    9i也可以
    select id, name,
           sum(case type when '正常' then hours else null end) 正常,
           sum(case type when '周末' then hours else null end) 周末,
           sum(case type when '假日' then hours else null end) 假日,
           sum(hours) total 
      from table1
    group by id,name