0

我正在尝试从表中获取所有行。我可以正确地完成这项工作。但是当我将每行值作为百分比并尝试对百分比求和时,问题就来了。例如,我使用以下查询来输出所有行:

SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
FROM nuexam
WHERE regd = '22'
GROUP BY subject

通过上述查询,我​​得到以下输出:

查询输出

我想总结一下Score。到目前为止,这是我可以输出的。很感谢任何形式的帮助。我也将欢迎 php 解决方案。

4

3 回答 3

3

只需运行嵌套查询即可:

SELECT SUM(s) as Score
FROM (SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS s
    FROM nuexam
    WHERE regd = '22'
    GROUP BY subject) table
于 2013-08-18T19:47:48.200 回答
3

如果您想获得个人分数以及总分,请使用with rollup. 您的数据的正确方法是:

select coalesce(subject, 'Total'), sum(t.Score)
from (SELECT subject, SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
      FROM nuexam
      WHERE regd = '22'
      GROUP BY subject
     ) t
group by subject with rollup;
于 2013-08-18T20:02:24.270 回答
2

http://sqlfiddle.com/#!2/a505a/4

select sum(t.Score) from 
(SELECT SUM( Mark_score ) / SUM( Full_mark ) *50 AS Score
FROM nuexam
WHERE regd = '22'
GROUP BY subject ) t
于 2013-08-18T19:54:57.123 回答