J'ai des procédures tirant des données de diverses sources pour me donner 3 tables très semblables.
Mesortingc | Tickets |Band ______________________________________ Acknowledgement | 45 | New Acknowledgement | 23 | Within Acknowledgement | 16 | Near Acknowledgement | 2 | Very Near
Et
Mesortingc | Tickets |Band ___________________________________ Escalation | 10 | New Escalation | 43 | Within Escalation | 81 | Near Escalation | 6 | Very Near
Et
Mesortingc| Tickets |Band ___________________________________ Fixed | 34 | New Fixed | 52 | Within Fixed | 36 | Near Fixed | 4 | Very Near
Maintenant, je voudrais les combiner set d'une manière ou d'une autre pour avoir une sortie de table comme celle-ci
Mesortingc | New | Within | Near | Very Near _____________________________________________________ Acknowledgement | 45 | 23 | 16 | 2 Escalation | 10 | 43 | 81 | 6 Fixed | 34 | 52 | 36 | 4
Comment puis-je y parvenir dans MS SQLServer, s'il vous plaît?
C'est un faux pivot qui devrait get ce que vous voulez. Vous devez ensuite associer cette requête à chaque table.
SELECT Mesortingc, MAX( CASE Band WHEN 'New' THEN Tickets ELSE '' END ) New, MAX( CASE Band WHEN 'Within' THEN Tickets ELSE '' END ) Within, MAX( CASE Band WHEN 'Near' THEN Tickets ELSE '' END ) Near, MAX( CASE Band WHEN 'Very Near' THEN Tickets ELSE '' END ) [Very Near] FROM table GROUP BY Mesortingc UNION ...
Cela ne nécessite pas de répéter tous les agrégats et les expressions CASE pour chaque table – juste une union simple fera, alors vous pouvez pivoter hors de cela.
SELECT Mesortingc, [New], [Within], [Near], [Very Near] FROM ( SELECT Mesortingc, Tickets, Band FROM dbo.table_a UNION ALL SELECT Mesortingc, Tickets, Band FROM dbo.table_b UNION ALL SELECT Mesortingc, Tickets, Band FROM dbo.table_c ) AS x PIVOT ( MAX(Tickets) FOR Band IN ([New],[Within],[Near],[Very Near]) ) AS p;