SQL Server how to select 2 table like this

zujrkrfu  于 2023-02-28  发布在  其他
关注(0)|答案(4)|浏览(182)

How to select table with condition like this.

I have a table that holds all kinds of subjects like this.

table subjects
Subjects idSubjects
01mathematics
02biology
03geography
04physics

then I also have a table to hold the value of each student like this.

table score
Student idSubjects idScore
10001018
10001026
10001037
10001049
10002015
10002027
100020310
10002047
10003016
10003027
10003038
10003049

I want to create a query with a form like the following table but i dont know how to make it.
| Student id | mathematics | biology | geography | physics |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 10001 | 8 | 6 | 7 | 9 |
| 10002 | 5 | 7 | 10 | 7 |
| 10003 | 6 | 7 | 8 | 9 |

please help me to solve this problem. Sorry my english is bad. I am still beginner

iyfamqjs

iyfamqjs1#

You have some ways to do that, but trying not to create temporary tables, you can do something like that:

select
    s.id,
    avg(case when sb.id = '01' then s.score end) as math,
    avg(case when sb.id = '02' then s.score end) as bio

from student s
join subject sb on (sb.id = s.subject_id)

group by s.id

Just fill the sum/case lines to the other subjects as you need!

Hope it helps.

62o28rlo

62o28rlo2#

This should work, assuming there is no duplicate in the score table, and those are the only 4 subjects you have. Or you can use PIVOT too.

SELECT
    sub.[Student Id]
    , CASE sc.[Subjects ID]
        WHEN '01' THEN sc.Score
        ELSE NULL
    AS mathematics
    , CASE sc.[Subjects ID]
        WHEN '02' THEN sc.Score
        ELSE NULL
    AS biology
    , CASE sc.[Subjects ID]
        WHEN '03' THEN sc.Score
        ELSE NULL
    AS geography
    , CASE sc.[Subjects ID]
        WHEN '04' THEN sc.Score
        ELSE NULL
    AS physics
FROM
    subjects sub 
    JOIN score sc ON sub.[Subjects ID] = sc.[Subjects ID]
r6l8ljro

r6l8ljro3#

You can use a group by like the other answers or left joins like this:

select
  students.id, 
  mat.score as mathematics,
  bio.score as biology,
  geo.score as geography,
  phy.score as physics
from (
  SELECT DISTINCT studentid as id
  from score
) as students
left join score as mat on mat.studentid = students.id and mat.subjectid = 1
left join score as bio on bio.studentid = students.id and bio.subjectid = 2
left join score as geo on geo.studentid = students.id and geo.subjectid = 3
left join score as phy on phy.studentid = students.id and phy.subjectid = 4
oipij1gg

oipij1gg4#

You can use:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Subjects) 
                    from subjects
                    group by Subjects
                    order by Subjects
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Student_id,' + @cols + ' 
            from 
             (
                select      D2.Student_id, D1.Subjects, D2.Score
                From        subjects D1
                Inner Join  score D2 
                    On      D1.Subjects_id = D2.Subjects_id
            ) x
            pivot 
            (
                sum(Score)
                for Subjects in (' + @cols + ')
            ) p '

execute(@query);

相关问题