请问聚合函数可不可以直接做加减运算?比如我查询各班学生总成绩两年的差,语句该怎么写?
要不要使用自连接什么的?比如成绩表有:学号,班级,姓名,年代,成绩select 班级,sum(s1.成绩)-sum(s2.成绩)
from 成绩表 s1, 成绩表s2
where s1.年=2009 and s2.年=2008
group by 班级
......不太会写,而且报错说第一行不是 GROUP BY 表达式请问高手们我应该怎么写啊?谢谢!

解决方案 »

  1.   

    给你一段代码参考一下,应该是一个原理
    SELECT a.itemtypeid,SUM(a.totalfee),SUM(b.totalfee) ,SUM(a.totalfee-b.totalfee)
     FROM expendituredetail a,expendituredetail b
     WHERE a.itemtypeid = b.itemtypeid
       AND a.input_date BETWEEN '20081001' AND '20081231'
       AND b.Input_Date BETWEEN '20090101' AND '20090210'
       GROUP BY a.itemtypeid
      

  2.   

    select s1.班级,sum(s1.成绩-s2.成绩) 成绩 
    from 成绩表 s1, 成绩表s2 
    where s1.班级=s2.班级 and s1.年=2009 and s2.年=2008 
    group by s1.班级 
      

  3.   


    select 班级,sum(s1.成绩 - s2.成绩) 
    from 成绩表 s1, 成绩表s2 
    where s1.年=2009 and s2.年=2008 and s1.班级 = s2.班级
    group by 班级