Question

Permettez-moi de décrire ce mieux que je peux, ce SQL Server est au sujet.

Il y a une table principale avec un enregistrement par participant, il y a une table sous avec au plus 5 enregistrements par participant.

Je dois être en mesure de remettre tous les enregistrements dans le tableau principal ainsi que les enregistrements de la table sous par participant, dans le même enregistrement dans la requête SELECT.

Exemple:

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

En gros les lignes à colonnes idée.

Comment puis-je obtenir ce fait? Je suis à une perte totale

Était-ce utile?

La solution

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'

Autres conseils

Vous devez faire un pivot ici. Mais le problème avec votre cas est que vous devez pivoter sur plus d'une colonne. J'espère que ce point vous dans la bonne direction multiple colonne Pivot dans T-SQL

SELECT *
FROM Main_Table, Sub_Table
WHERE Main_Table.Participant_ID = Sub_Table.Participant_ID;

Est-ce que SQL Server utilise SQL standard?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top