例子:
年度  月份  數量
2006   01    12
2006   02    13
2006   05    16
2007   05    23
2007   08    23
2008   12    23結果:
年度   月份   數量   月份 數量   月份 數量
2006    01     12     02   13     05   16
2007    05     23     08   23
2008    12請問,要達到這樣的效果,應該怎么做,sql中是不是有這樣的函數,但我不會用.

解决方案 »

  1.   

    SELECT year,
    max(DECODE(month,'01',totol,0)) 01,
    max(DECODE(month,'02',totol,0)) 02,
    max(DECODE(month,'03',totol,0)) 03,
    max(DECODE(month,'04',totol,0)) 04
    FROM table group by year;
    你自己测试一下,思路是这样的
      

  2.   

    你好,我是在SQL SERVER 2005中的,沒有DECODE這個函數,請問要怎么解決呢
      

  3.   

    知道decode和case 的区别么?用case 一样一样的
    给你写个完整的吧,sqlserver 没怎么用,case应该还是有的
    select syear,max(case when smonth = '01' then smonth  end),  --01
           max(case when smonth = '01' then totol  end),         --12  
           max(case when smonth = '02' then smonth  end),        --02
           max(case when smonth = '02' then totol  end),         --13 
           max(case when smonth = '03' then smonth  end),
           max(case when smonth = '03' then totol  end), 
           max(case when smonth = '04' then smonth  end),
           max(case when smonth = '04' then totol  end), 
           max(case when smonth = '05' then smonth  end),
           max(case when smonth = '05' then totol end), 
           max(case when smonth = '06' then smonth  end),
           max(case when smonth = '06' then totol  end), 
           max(case when smonth = '07' then smonth  end),
           max(case when smonth = '07' then totol  end), 
           max(case when smonth = '08' then smonth  end),
           max(case when smonth = '08' then totol  end),
           max(case when smonth = '09' then smonth  end),
           max(case when smonth = '09' then totol  end), 
           max(case when smonth = '10' then smonth  end),
           max(case when smonth = '10' then totol end), 
           max(case when smonth = '11' then smonth  end),
           max(case when smonth = '11' then totol  end), 
           max(case when smonth = '12' then smonth  end),
           max(case when smonth = '12' then totol  end)
           from abc 
           group by syear