如何使用SQL将多行值作为具有多列的单行返回

时间:2018-09-13 10:20:29

标签: sql

静态表中有称为“ SUBJECT”的三行

SubjectID       SubjectCode
---------------------------
    1           MATHS
    2           SCIENCE
    3           ENGLISH
---------------------------

我的应用程序会将相应学生的每个科目分录放在另一个称为“ MARKS”的表中

MarkID      StudentID       SubjectID       Marks
---------------------------------------------------
    1           1               1           90
    2           1               2           85
    3           1               3           80
    4           2               1           100
    5           2               2           70
    6           2               3           90

我正在努力创建一个如下所示的虚拟表:

StudentID       Maths       Science     English 
------------------------------------------------
    1           90          85          80
    2           100         70          90

我尝试了查询

Select  MARKS.StudentID,
        CASE WHEN SUBJECTS.SubjectCode = 'MATHS' THEN MARKS.Marks END as Maths, 
        CASE WHEN SUBJECTS.SubjectCode = 'SCIENCE' THEN MARKS.Marks END as Science, 
        CASE WHEN SUBJECTS.SubjectCode = 'ENGLISH' THEN MARKS.Marks END as English, 
        FROM 
        MARKS 
        JOIN SUBJECTS on SUBJECTS.SubjectID = MARKS.SubjectID
        GROUP BY 
        MARKS.StudentID, SUBJECTS.SubjectCode, MARKS.Marks

但它会返回

StudentID       Maths       Science     English 
------------------------------------------------
    1           90          NULL        NULL
    1           NULL        85          NULL
    1           NULL        NULL        80
    2           100         NULL        NULL
    2           NULL        70          NULL
    2           NULL        NULL        90

3 个答案:

答案 0 :(得分:4)

您很近。您只需要聚合:

SELECT MARKS.StudentID,
       MAX(CASE WHEN SUBJECTS.SubjectCode = 'MATHS' THEN MARKS.MARK END) as Maths, 
       MAX(CASE WHEN SUBJECTS.SubjectCode = 'SCIENCE' THEN MARKS.MARK END) as Science, 
       MAX(CASE WHEN SUBJECTS.SubjectCode = 'ENGLISH' THEN MARKS.MARK END) as English, 
FROM MARKS JOIN
     SUBJECTS 
     ON SUBJECTS.SubjectID = MARKS.SubjectID
GROUP BY MARKS.StudentID
ORDER BY MARKS.StudentID;

我建议您使用表别名,以便查询更易于编写和读取:

SELECT m.StudentID,
       MAX(CASE WHEN s.SubjectCode = 'MATHS' THEN M.MARK END) as Maths, 
       MAX(CASE WHEN s.SubjectCode = 'SCIENCE' THEN M.MARK END) as Science, 
       MAX(CASE WHEN s.SubjectCode = 'ENGLISH' THEN M.MARK END) as English, 
FROM MARKS m JOIN
     SUBJECTS s
     ON s.SubjectID = m.SubjectID
GROUP BY m.StudentID
ORDER BY m.StudentID;

答案 1 :(得分:3)

使用分组依据和汇总

Select  MARKS.StudentID,
        max(CASE WHEN SUBJECTS.SubjectCode = 'MATHS' THEN RS.AgentPercentage END) as Maths, 
        max(CASE WHEN SUBJECTS.SubjectCode = 'SCIENCE' THEN RS.AgentPercentage END) as Science, 
        max(CASE WHEN SUBJECTS.SubjectCode = 'ENGLISH' THEN RS.AgentPercentage END) as English, 
        FROM 
        MARKS 
        JOIN SUBJECTS on SUBJECTS.SubjectID = MARKS.SubjectID
group by MARKS.StudentID

答案 2 :(得分:1)

如果使用的是Pivot,则可以使用sql-server

DECLARE @SUBJECT TABLE (SubjectID INT,  SubjectCode VARCHAR(10))
INSERT INTO @SUBJECT VALUES 
(1 ,'MATHS'),
(2 ,'SCIENCE'),
(3 ,'ENGLISH')

DECLARE @MARKS TABLE (MarkID INT, StudentID INT, SubjectID INT, Marks INT)
INSERT INTO @MARKS VALUES
(1 ,1, 1, 90 ),
(2 ,1, 2, 85 ),
(3 ,1, 3, 80 ),
(4 ,2, 1, 100),
(5 ,2, 2, 70 ),
(6 ,2, 3, 90 )

SELECT * FROM (
    SELECT M.StudentID, M.Marks, S.SubjectCode FROM @MARKS M 
        INNER JOIN @SUBJECT S ON S.SubjectID = M.SubjectID) SRC
PIVOT (MAX(Marks) FOR SubjectCode IN ([MATHS],[SCIENCE],[ENGLISH])) PVT

结果:

StudentID   MATHS       SCIENCE     ENGLISH
----------- ----------- ----------- -----------
1           90          85          80
2           100         70          90