如何从此表中选择并添加平均值然后排序[重复]
Posted
技术标签:
【中文标题】如何从此表中选择并添加平均值然后排序[重复]【英文标题】:how to select from this table and add average value and then sort [duplicate] 【发布时间】:2012-04-24 20:01:45 【问题描述】:可能重复:how to select, average and sort in mysql table
我在 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
【问题讨论】:
【参考方案1】:select * from <your query> order by PERC
至极:
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
【讨论】:
以上是关于如何从此表中选择并添加平均值然后排序[重复]的主要内容,如果未能解决你的问题,请参考以下文章