How to pivot rows into columns (custom pivoting)

前端 未结 6 1818
夕颜
夕颜 2020-11-27 05:27

I have a Sql Database table similar to the following:

Day   Period    Subject

Mon   1         Ch
Mon   2         Ph
Mon   3         Mth
Mon   4         CS
M         


        
6条回答
  •  自闭症患者
    2020-11-27 05:43

    You could try...

    SELECT DISTINCT Day,
           (SELECT Subject
                FROM my_table mt2
                WHERE mt2.Day = mt.Day AND
                      Period  = 1) AS P1,
           (SELECT Subject
                FROM my_table mt2
                WHERE mt2.Day = mt.Day AND
                      Period  = 2) AS P2,
       .
       .
       etc
       .
       .
       .
       (SELECT Subject
            FROM my_table mt2
            WHERE mt2.Day = mt.Day AND
                  Period  = 7) AS P7
    FROM my_table mt;
    

    but I can't say I like it very much. Better than nothing, though.

提交回复
热议问题