SQL Server - 一个查询中的多个计数结果问题

时间:2017-04-26 08:43:20

标签: sql sql-server sql-server-2012

我正在尝试使用SELECT查询来计算学生的成绩。这基于3个表:

  • 学生(StudentID PRIMARY KEY)
  • 成绩(GradeID PRIMARY KEY)
  • GradeLine(GradeLineID PRIMARY KEY,StudentID FOREIGN KEY,GradeID FOREIGN KEY)

问题:当使用下面的查询时,如果其中一个条件为null(例如Student 3没有区别,但在所有其他列中都有标记),那么Student是从查询中删除,我希望它们仍然存在,只显示0而不是

SELECT
    GradeLine.StudentID,
    COUNT(CASE WHEN GradeLine.GradeID = 1 THEN (GradeLine.GradeID) END) AS Distinction,
    COUNT(CASE WHEN GradeLine.GradeID = 2 THEN (GradeLine.GradeID) END) AS  Merit,
    COUNT(CASE WHEN GradeLine.GradeID = 3 THEN (GradeLine.GradeID) END) AS Pass,
    COUNT(CASE WHEN GradeLine.GradeID = 4 THEN (GradeLine.GradeID) END) AS Fail,
    COUNT (GradeLine.GradeID) AS Total
FROM 
    GradeLine
GROUP BY 
    GradeLine.StudentID

预期的示例数据:

StudentID   Distinction Merit   Pass    Fail    Total
------------------------------------------------------
   1             1         3     4       2       10
   2             1         7     2       1       11
   3             0         3     3       5       11
   4             0        12     0       0       12
   5             6         3     0       0        9

有人可以告诉我我做错了什么吗?

新的当前结果:

StudentID   Distinction Merit   Pass    Fail    Total
-----------------------------------------------------
   1             0        0      10      0       10
   2            11        0       0      0       11
   3             0        0       0     11       11
   4             0       12       0      0       12
   5             0        0       0      9        9

表格代码,约束和样本数据:

--CREATE TABLES
CREATE TABLE Student (StudentID INT IDENTITY (1,1) NOT NULL,
Studentname VARCHAR(50))

CREATE TABLE Grade (GradeID INT IDENTITY (1,1) NOT NULL,
Gradename VARCHAR(50))

CREATE TABLE GradeLine (GradeLineID INT IDENTITY (1,1) NOT NULL,
StudentID INT,
GradeID INT)

--PK CONSTRAINTS
ALTER TABLE Student ADD CONSTRAINT StudentID_PK PRIMARY KEY (StudentID)
ALTER TABLE Grade ADD CONSTRAINT GradeID_PK PRIMARY KEY (GradeID)
ALTER TABLE GradeLine ADD CONSTRAINT GradeLine_PK PRIMARY KEY (GradeLineID)

--FK CONSTRAINTS
ALTER TABLE GradeLine ADD CONSTRAINT StudentID_GL2S FOREIGN KEY (StudentID) REFERENCES Student (StudentID)
ALTER TABLE GradeLine ADD CONSTRAINT GradeID_GL2G FOREIGN KEY (GradeID) REFERENCES Grade (GradeID)

--DATA 
INSERT INTO Student VALUES ('Student A')
INSERT INTO Student VALUES ('Student B')
INSERT INTO Student VALUES ('Student C')

INSERT INTO Grade VALUES ('Distinction')
INSERT INTO Grade VALUES ('Merit')
INSERT INTO Grade VALUES ('Pass')
INSERT INTO Grade VALUES ('Fail')

--STUDENT A 
INSERT INTO GradeLine VALUES (1, 1) --STUDENT A DISTINCTION
INSERT INTO GradeLine VALUES (1, 1) --STUDENT A DISTINCTION
INSERT INTO GradeLine VALUES (1, 2) --STUDENT A MERIT

--STUDENT B
INSERT INTO GradeLine VALUES (2, 1) --STUDENT A DISTINCTION
INSERT INTO GradeLine VALUES (2, 1) --STUDENT A DISTINCTION
INSERT INTO GradeLine VALUES (2, 2) --STUDENT A MERIT
INSERT INTO GradeLine VALUES (2, 5) --STUDENT A FAIL

--STUDENT C
INSERT INTO GradeLine VALUES (3, 2) --STUDENT A MERIT
INSERT INTO GradeLine VALUES (3, 3) --STUDENT A PASS
INSERT INTO GradeLine VALUES (3, 4) --STUDENT A FAIL

1 个答案:

答案 0 :(得分:1)

您可以使用PIVOT

SELECT      piv.StudentID,
            piv.[1] AS Distinction,
            piv.[2] AS Merit,
            piv.[3] AS Pass,
            piv.[4] AS Fail,
            (piv.[1] + piv.[2] + piv.[3] + piv.[4]) AS Total
FROM 
(
  SELECT    StudentID, GradeID
  FROM      GradeLine
) src
PIVOT
(
  COUNT(GradeID)
  FOR GradeID IN ([1], [2], [3], [4])
) piv;

这应该在COUNT为0的情况下开箱即用。

你可以在这里检查整个事情 - > http://rextester.com/NUER1494

希望这有帮助!!!