我在mySql中有一张表,如图所示
我想写一个查询,结果将按LESSON列分组,并添加新行,即LESSON列的平均值和总和CNT列值....
我使用这个查询但它给出的结果如图3所示,在这种情况下我不能按PERC排序
select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON
答案 0 :(得分:0)
select * from <your query> order by PERC
wich:
select * from (
select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON
) as sub order by PERC DESC