2016-06-30 4 views
0

안녕하세요 피벗 해제 및 피벗 데이터를 적용했습니다. 모든 것이 잘되고 있습니다. unpivot의 "IN"절에 지정된 것과 동일한 순서로 출력을 정렬하려고합니다. 도와주세요. 여기에 지금까지 무엇을했는지입니다 :이 순서대로 결과를 필요SQL Server에서 피벗 해제 및 피벗 정렬 순서를 수동으로 설정하는 방법

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 

: [TrainingDoneThisMonth], [FoodQualityStatus가], [NoOfAllDrugTests], [NoOfAllAlcoholTests]

나는 시도했다 : SQL Server , restrict UNPIVOT to order columns automatically 하지만 작동시키지 못했습니다.

+0

이'당신의 정렬 순서에 최상위 쿼리에서 'SELECT * 변경해보십시오. 입력 할 열이 많지 않습니다. – Kramb

답변

1

이 시도 :

 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 #my 테이블 철자 실수 DROP TABLE #myTable –

+0

@KhanImranAli 응? 나는 카페인이 더 필요할지도 모르지만 철자법 실수를 볼 수는 없다. –

+1

감사합니다. "Arthur D"는 부드러운 범죄자처럼 작동합니다. @KhanImranAli 야, 너 좀 쉬어야 해. –