2016-06-30 2 views
0

Salut J'ai appliqué Unpivot et pivoter vers mes données. Tout va bien, sauf que je veux organiser ma sortie dans le même ordre que spécifié dans la clause "IN" d'unpivot. S'il vous plaît aider. Voici ce que je l'ai fait jusqu'à présent:Comment faire pour définir manuellement l'ordre de tri d'Unpivot et pivoter dans le serveur SQL

CREATE TABLE #myTable 
    (
    [ForYear] [smallint] NOT NULL, 
    [ForMonth] [tinyint] NOT NULL, 
    [TrainingDoneThisMonth] [bit] NULL, 
    [FoodQualityStatus] [bit] NULL, 
    [NoOfAllDrugTests] [int] NULL, 
    [NoOfAllAlcoholTests] [int] NULL 
    ) 


    INSERT INTO #myTable 
    values 
    (2016,1,1,0,5,10), 
    (2016,2,0,1,15,5), 
    (2016,3,1,0,20,15), 
    (2016,4,0,1,5,25), 
    (2016,5,1,0,10,30), 
    (2015,1,1,0,5,10), 
    (2015,2,0,1,15,5), 
    (2015,3,1,0,20,15), 
    (2015,4,0,1,5,25), 
    (2015,5,1,0,10,30) 
select * from(SELECT * 
    FROM (
     SELECT DATENAME(month,DATEADD(month,[ForMonth]-1,'1970-01-01')) as d, 
       CAST([TrainingDoneThisMonth] as int) as [TrainingDoneThisMonth], 
       CAST([FoodQualityStatus] as int) as [FoodQualityStatus], 
       [NoOfAllDrugTests], 
       [NoOfAllAlcoholTests] 

     FROM #myTable 
     WHERE foryear=2016 
     ) d 
    UNPIVOT (
     [VALUES] FOR [Objective] in ([TrainingDoneThisMonth],[FoodQualityStatus],[NoOfAllDrugTests],[NoOfAllAlcoholTests]) 
    ) unpvt 
) as p 
PIVOT (
    SUM([VALUES]) FOR d IN ([January],[February],[March],[April],[May]) 
) as pvt 

Je dois le résultat dans cet ordre: [TrainingDoneThisMonth], [FoodQualityStatus], [NoOfAllDrugTests], [NoOfAllAlcoholTests]

J'ai essayé: SQL Server , restrict UNPIVOT to order columns automatically mais incapable de le faire fonctionner.

+0

Essayez de changer votre '' SELECT * dans la partie supérieure le plus à votre requête ordre de tri. Il n'y a pas beaucoup de colonnes à taper. – Kramb

Répondre

1

Essayez ceci:

 CREATE TABLE #myTable 
     (
     [ForYear] [smallint] NOT NULL, 
     [ForMonth] [tinyint] NOT NULL, 
     [TrainingDoneThisMonth] [bit] NULL, 
     [FoodQualityStatus] [bit] NULL, 
     [NoOfAllDrugTests] [int] NULL, 
     [NoOfAllAlcoholTests] [int] NULL 
     ) 


     INSERT INTO #myTable 
     values 
     (2016,1,1,0,5,10), 
     (2016,2,0,1,15,5), 
     (2016,3,1,0,20,15), 
     (2016,4,0,1,5,25), 
     (2016,5,1,0,10,30), 
     (2015,1,1,0,5,10), 
     (2015,2,0,1,15,5), 
     (2015,3,1,0,20,15), 
     (2015,4,0,1,5,25), 
     (2015,5,1,0,10,30) 
    select *, 
      CASE WHEN objective = 'TrainingDoneThisMonth' THEN 1 
       WHEN objective = 'FoodQualityStatus' THEN 2 
       WHEN objective = 'NoOfAllDrugTests' THEN 3 
       WHEN objective = 'NoOfallAlcoholTests' THEN 4 
      ELSE 5 END AS [ranking] 
from(SELECT * 
     FROM (
      SELECT DATENAME(month,DATEADD(month,[ForMonth]-1,'1970-01-01')) as d, 
        CAST([TrainingDoneThisMonth] as int) as [TrainingDoneThisMonth], 
        CAST([FoodQualityStatus] as int) as [FoodQualityStatus], 
        [NoOfAllDrugTests], 
        [NoOfAllAlcoholTests] 

      FROM #myTable 
      WHERE foryear=2016 
      ) d 
     UNPIVOT (
      [VALUES] FOR [Objective] in ([TrainingDoneThisMonth],[FoodQualityStatus],[NoOfAllDrugTests],[NoOfAllAlcoholTests]) 
    ) unpvt 
) as p 
PIVOT (
    SUM([VALUES]) FOR d IN ([January],[February],[March],[April],[May]) 
) as pvt 
ORDER BY ranking 
DROP TABLE #myTable 
+0

DROP TABLE #myTable erreur de calcul DROP TABLE #myTable –

+0

@KhanImranAli hein? Je pourrais avoir besoin de plus de caféine mais je ne peux pas voir l'erreur d'orthographe –

+1

Merci "Arthur D" fonctionne comme un criminel lisse. @KhanImranAli Mec vous avez besoin de repos. –