Colonnes d'unpivot SQL Server

J'ai une table que je voudrais unpivot dans une instruction SQL. Il se compose d'une personne et du téléphone 1 à 5. En ce moment, je fais un syndicat pour chaque téléphone, mais je crains que cela ne cause des problèmes de performance.

Colonnes:

PERSON_GUID, PHONE_1, PHONE_1_VOICE_FLG, PHONE_2, PHONE_2_VOICE_FLG, PHONE_3, PHONE_3_VOICE_FLG, PHONE_4, PHONE_4_VOICE_FLG, PHONE_5, PHONE_5_VOICE_FLG 

Comment est-ce que je devrais améliorer la rangée avec la performance en tête pour que les résultats soient:

 PERSON_GUID, PHONE_NO, VOICE_FLG 

Je préfère UNPIVOT mais quant à votre solution –
Assurez-vous que vous utilisez UNION ALL et non UNION .
UNION ALL renvoie juste un résultat de requête après l'autre.
UNION élimine les duplications de lignes et c'est là que vous payez en performance.


 select PERSON_GUID,PHONE_NO, case right(col,1) when 1 then PHONE_1_VOICE_FLG when 2 then PHONE_2_VOICE_FLG when 3 then PHONE_3_VOICE_FLG when 4 then PHONE_4_VOICE_FLG when 5 then PHONE_5_VOICE_FLG end VOICE_FLG from t unpivot (PHONE_NO for col in (PHONE_1,PHONE_2,PHONE_3,PHONE_4,PHONE_5)) u