如何在 sql 服务器中手动设置 Unpivot 和 pivot 的排序顺序

How to manually set sort order of Unpivot and pivot in sql server

您好,我已将 Unpivot 和 Pivot 应用于我的数据。一切顺利,除了我想按照 unpivot 的 "IN" 子句中指定的相同顺序排列我的输出。请帮忙。这是我到目前为止所做的:

    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 但无法使其正常工作。

试试这个:

      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
    create table #objective
              (obj varchar(100),ord int)

    insert into #objective (obj,ord) values('TrainingDoneThisMonth',1)
    insert into #objective (obj,ord) values('FoodQualityStatus',2)

    insert into #objective (obj,ord) values('NoOfAllDrugTests',3)
`  `insert into #objective (obj,ord) values('NoOfAllDrugTests',4)

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 * into #ajay
    --, 
    --      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 

select #ajay.*,ord from #ajay
join objective on objective.obj=#ajay.[Objective]
order by ord
DROP TABLE #myTable
DROP TABLE #ajay