TSQL 枢轴开关

TSQL Pivot Switch

我正在尝试将 DataDescription 行转换为列,并将 Number* 列取消转换为别名为新列的行。

所需的列为:

Location, 2016-01-01, 2016-01-02, 2016-01-03, 2016-01-04, 2016-01-05,CountType

下面是设置场景的代码。这会在 C# 或 TSQL 中更干净地完成吗?有什么建议吗?

if (object_id('tempdb..#data') is not null)
    begin
        drop table #data
    end

    create table #data
    (
        DateDescription VARCHAR(50),
        Location VARCHAR(50),
        NumberOfVisits INT,
        NumberOfPositiveVisits INT,
        NumberOfNegativeVisits INT
    )

    insert into #data
    SELECT '2016-01-01', 'SiteA', 100, 80, 20
    union all 
    SELECT '2016-01-02', 'SiteA', 95, 81, 21
    union all 
    SELECT '2016-01-03', 'SiteA', 95, 81, 21
    union all 
    SELECT '2016-01-04', 'SiteA', 95, 81, 21
    union all 
    SELECT '2016-01-05', 'SiteA', 95, 81, 21
    union all 
    SELECT '2016-01-01', 'SiteB', 95, 81, 21
    union all 
    SELECT '2016-01-02', 'SiteB', 95, 81, 21
    union all 
    SELECT '2016-01-03', 'SiteB', 95, 81, 21
    union all 
    SELECT '2016-01-04', 'SiteB', 95, 81, 21
    union all 
    SELECT '2016-01-05', 'SiteB', 95, 81, 21

    select * from #data

如果您正在寻找 T-Sql 解决方案,您可能想要深入了解 Pivot 功能。不确定您对 CountType 列有什么期望,但可以通过以下方式进行分组:

SELECT Location
      ,[2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
      ,[Type]
FROM (
    SELECT [DateDescription]
          ,Location
          ,NumberOfVisits as visits
          ,'Total' AS [Type]
    FROM #data) AS Source
PIVOT
(
    SUM(visits)
    FOR
    DateDescription IN
    (
       [2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
    )
) AS PivotTable

UNION    

SELECT Location
      ,[2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
      ,[Type]
FROM (
    SELECT [DateDescription]
          ,Location
          ,NumberOfPositiveVisits as visits
          ,'Positive' AS [Type]
    FROM #data) AS Source
PIVOT
(
    SUM(visits)
    FOR
    DateDescription IN
    (
       [2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
    )
) AS PivotTable

UNION 

    SELECT Location
      ,[2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
      ,[Type]
FROM (
    SELECT [DateDescription]
          ,Location
          ,NumberOfNegativeVisits as visits
          ,'Negative' AS [Type]
    FROM #data) AS Source
PIVOT
(
    SUM(visits)
    FOR
    DateDescription IN
    (
       [2016-01-01]
      ,[2016-01-02]
      ,[2016-01-03]
      ,[2016-01-04]
      ,[2016-01-05]
    )
) AS PivotTable;

像这样的东西会起作用:

SELECT
    Location
    , [2016-01-01]
    , [2016-01-02]
    , [2016-01-03]
    , [2016-01-04]
    , [2016-01-05]
    , CountType
FROM
    (
        SELECT
            DateDescription
            , Location
            , [Count]
            , CountType
        FROM
            (
                SELECT
                    DateDescription
                    , Location
                    , NumberOfVisits
                    , NumberOfPositiveVisits
                    , NumberOfNegativeVisits
                FROM #data
            ) Q
            UNPIVOT
            (
                [Count]
                FOR CountType IN ([NumberOfVisits], [NumberOfPositiveVisits], [NumberOfNegativeVisits])
            ) U
    ) Q
    PIVOT
    (
        MAX([Count])
        FOR DateDescription IN ([2016-01-01], [2016-01-02], [2016-01-03], [2016-01-04], [2016-01-05])
    ) P
ORDER BY
    Location
    , CountType DESC