用标准sql语句实现
year month total
1996 1 3000
1996 3 4000
1996 7 5000
1997 3 4000
1997 6 3000
.
.
year m1,m2,m3,m4year 为年,m1等为季度,要求按上行输出

解决方案 »

  1.   

    select year,
    sum(case when month between 1 and 3 then total else 0 end ) m1,
    sum(case when month between 4 and 6 then total else 0 end ) m2,
    sum(case when month between 7 and 9 then total else 0 end ) m3,
    sum(case when month between 10 and 12 then total else 0 end ) m4
    from table group by year
      

  2.   

    非常感谢,这道题是不是不用case作不了啊
      

  3.   

    还可以用DECODE~不过原理是一样的,换种写法而已~
      

  4.   

    select year, 
    sum(decode(ceil(month / 3),1,total)) as m1, 
    sum(decode(ceil(month / 3),2,total)) as m2, 
    sum(decode(ceil(month / 3),3,total)) as m3, 
    sum(decode(ceil(month / 3),4,total)) as m4 
    from yourtable group by year;
      

  5.   

    在oracle8上case用不了,decode可以。