SQL Server - multiple counts in one query results issue

Crezzer7

I am trying to count the grades of students using a SELECT query. This is based on 3 tables:

  • Student (StudentID PRIMARY KEY)
  • Grade (GradeID PRIMARY KEY)
  • GradeLine (GradeLineID PRIMARY KEY, StudentID FOREIGN KEY, GradeID FOREIGN KEY)

The problem: when using the below query, if one of the criteria is null (example being Student 3 has no distinctions, but has a mark in all other columns), then that Student is removed from the query, I want them to still be present and just display a 0 instead

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

Example data expected:

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

Please could someone advise me on what I am doing wrong?

NEW current results:

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

Table Code, Constraints and Sample Data:

--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
Satwik Nadkarny

You can make use of 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;

This should work out of the box where the COUNT is 0.

You can check the entire thing in action here -> http://rextester.com/NUER1494

Hope this helps!!!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get multiple counts with one SQL query?

From Dev

create multiple counts in one SQL query

From Dev

SQL Server - Merge multiple query results into one result set

From Dev

SQL Server : query to get multiple results in one field

From Dev

SQL Server : query to get multiple results in one field

From Dev

SQL Server - Merge multiple query results into one result set

From Dev

SQL Server Multiple Counts in the same Query

From Dev

Linq to SQL to return multiple counts from query not returning correct results

From Dev

How to combine different counts in one SQL Server query

From Dev

Creating an SQL query with multiple counts and different criteria from one table

From Dev

Multiple months counts in one query

From Dev

Use multiple SQL query results in one SQL query

From Dev

SQL Server How to output one table result from multiple results with a WHILE query

From Dev

Multiple counts in a single SQL query

From Dev

Issue with sql query to get the results

From Dev

How to do multiple counts / exists in one query?

From Dev

SQL Server : flatten results from One to Many query

From Dev

SQL Server How to output multiple results into one table

From Dev

SQL - Displaying 3 different counts on one query

From Dev

Write custom SQL query for multiple counts

From Dev

Print counts from multiple tables in one SQL

From Dev

Multiple counts on the same SQL Server 2012 table

From Dev

SQL Server query results as INSERT

From Dev

Transpose SQL Server Query Results

From Dev

How to extract results from multiple query results tabs into one excel in SQL Developer?

From Dev

Apply multiple "Where" clauses in one Query SQL SERVER

From Dev

sql server shred xml with multiple nodes in one query

From Dev

Get values from multiple sql server table by one query

From Dev

SQL query to fetch all results in one query

Related Related

  1. 1

    How to get multiple counts with one SQL query?

  2. 2

    create multiple counts in one SQL query

  3. 3

    SQL Server - Merge multiple query results into one result set

  4. 4

    SQL Server : query to get multiple results in one field

  5. 5

    SQL Server : query to get multiple results in one field

  6. 6

    SQL Server - Merge multiple query results into one result set

  7. 7

    SQL Server Multiple Counts in the same Query

  8. 8

    Linq to SQL to return multiple counts from query not returning correct results

  9. 9

    How to combine different counts in one SQL Server query

  10. 10

    Creating an SQL query with multiple counts and different criteria from one table

  11. 11

    Multiple months counts in one query

  12. 12

    Use multiple SQL query results in one SQL query

  13. 13

    SQL Server How to output one table result from multiple results with a WHILE query

  14. 14

    Multiple counts in a single SQL query

  15. 15

    Issue with sql query to get the results

  16. 16

    How to do multiple counts / exists in one query?

  17. 17

    SQL Server : flatten results from One to Many query

  18. 18

    SQL Server How to output multiple results into one table

  19. 19

    SQL - Displaying 3 different counts on one query

  20. 20

    Write custom SQL query for multiple counts

  21. 21

    Print counts from multiple tables in one SQL

  22. 22

    Multiple counts on the same SQL Server 2012 table

  23. 23

    SQL Server query results as INSERT

  24. 24

    Transpose SQL Server Query Results

  25. 25

    How to extract results from multiple query results tabs into one excel in SQL Developer?

  26. 26

    Apply multiple "Where" clauses in one Query SQL SERVER

  27. 27

    sql server shred xml with multiple nodes in one query

  28. 28

    Get values from multiple sql server table by one query

  29. 29

    SQL query to fetch all results in one query

HotTag

Archive