1

让我尽可能地描述一下,这是关于 SQL Server 的。

有一个主表,每个参与者有一条记录,有一个子表,每个参与者最多有 5 条记录。

我需要能够在 SELECT 查询的同一记录中返回主表中的所有记录以及每个参与者的子表记录。

例子:

Main Table:
   Participant_ID,
   Program
Sub Table:
   Participant_ID,
   Skill_Set_ID,
   Rating

SQL Query results:
    Participant_ID, Program, Skill_Set_ID_1, Rating_1, Skill_Set_ID_2, Rating_2, Skill_Set_ID_3, Rating_3, Skill_Set_ID_4, Rating_4, Skill_Set_ID_5, Rating_5

基本上行到列的想法。

我怎样才能完成这项工作?我完全不知所措

4

2 回答 2

1
select mt.Participant_ID, mt.Program, 
       st1.Skill_Set_ID as Skill_Set_ID_1, st1.Rating as Rating_1, 
       st2.Skill_Set_ID as Skill_Set_ID_2, st2.Rating as Rating_2, 
       st3.Skill_Set_ID as Skill_Set_ID_3, st3.Rating as Rating_3, 
       st4.Skill_Set_ID as Skill_Set_ID_4, st4.Rating as Rating_4, 
       st5.Skill_Set_ID as Skill_Set_ID_5, st5.Rating as Rating_5, st5.Skill_Text
       st6.Skill_Set_ID as Skill_Set_ID_6, st6.Rating as Rating_6, st6.Skill_Text
       st7.Skill_Set_ID as Skill_Set_ID_7, st5.Rating as Rating_7, st7.Skill_Text
    from main_table mt
        left join sub_table st1
            on mt.Participant_ID = st1.Paticipant_ID
                and st1.Skill_Set_ID = 1
        left join sub_table st2
            on mt.Participant_ID = st2.Paticipant_ID
                and st2.Skill_Set_ID = 2
        left join sub_table st3
            on mt.Participant_ID = st3.Paticipant_ID
                and st3.Skill_Set_ID = 3
        left join sub_table st4
            on mt.Participant_ID = st4.Paticipant_ID
                and st4.Skill_Set_ID = 4
        left join sub_table st5
            on mt.Participant_ID = st5.Paticipant_ID
                and st5.Skill_Set_ID = 5
                and st5.Skill_Text = 'Skill A'
        left join sub_table st6
            on mt.Participant_ID = st6.Paticipant_ID
                and st6.Skill_Set_ID = 5
                and st6.Skill_Text = 'Skill B'
        left join sub_table st7
            on mt.Participant_ID = st7.Paticipant_ID
                and st7.Skill_Set_ID = 5
                and st7.Skill_Text = 'Skill C'
于 2010-11-08T20:46:06.680 回答
-1
SELECT *
FROM Main_Table, Sub_Table
WHERE Main_Table.Participant_ID = Sub_Table.Participant_ID;

SQL Server 是否使用标准 SQL?

于 2010-11-08T19:20:44.147 回答