0

第一张图片是我的查询输出。现在我想对主题进行分组,使其变得像第二张图像。可能吗?谢谢您的帮助。

select Subject, Grade, case when Grade >= 50 Then '1' else '0' end as Pass, case when Grade < 50 Then '1' else '0' end as Fail from Grade_report

输出:

在此处输入图像描述

我想要的是:

在此处输入图像描述

4

1 回答 1

0

您的规格不是很准确;您希望合并记录的分数是多少,应该只写 1 或 0 还是汇总通过和失败列的总和?

以下将生成您的输出,它需要MAX成绩和SUM通过/失败信息:

WITH GradePassFail AS (
    SELECT 
        Subject, 
        Grade, 
        CASE WHEN Grade >= 50 THEN 1 ELSE 0 END AS Pass, 
        CASE WHEN Grade < 50 THEN 1 ELSE 0 END AS Fail 
    FROM Grade_report
)
SELECT Subject, MAX(Grade) AS Grade, SUM(Pass) AS Pass, SUM(Fail) AS Fail
FROM GradePassFail
GROUP BY Subject
于 2012-08-04T18:55:24.693 回答