如何将行转置为具有动态值的SQL或SSIS中的列

时间:2014-07-04 19:36:48

标签: sql sql-server excel ssis transpose

我有以下表格,其中保存了每日数据:

Data generated by SQL Query

我需要阅读最近7天的数据并将行转换为列以将其保存在Excel报表中。我试图用SSIS自动化这个。 请注意,这里在转置后表现为列的日期将基于过去七天是动态的,所以我不能在pivot函数中使用静态列名。 所需的输出如下所示:

Desired output format

请告知如何通过MS-SQL或SSIS完成输出。提前致谢。以下是表模式和示例数据:

IF OBJECT_ID('tempdb..#PriorityTrends') IS NOT NULL DROP TABLE #PriorityTrends  

CREATE TABLE #PriorityTrends (
    CountDate DATETIME,
    [Priority 1] INT,
    [Priority 2] INT,
    [Priority 3] INT,
    [Priority 4] INT,
    [Priority 5] INT,
    Total INT
    )

INSERT INTO #PriorityTrends
VALUES ('6/24/2014', 163, 235, 741, 265, 1932, 1404)

INSERT INTO #PriorityTrends
VALUES ('6/25/2014', 174, 368, 775, 256, 2634, 1573)

INSERT INTO #PriorityTrends
VALUES ('6/26/2014', 201, 448, 703, 270, 2717, 1622)

INSERT INTO #PriorityTrends
VALUES ('6/27/2014', 140, 464, 612, 257, 1776, 1473)

INSERT INTO #PriorityTrends
VALUES ('6/30/2014', 106, 333, 1274, 311, 1480, 2024)

INSERT INTO #PriorityTrends
VALUES ('7/1/2014', 164, 834, 1533, 1145, 10483, 3676)

INSERT INTO #PriorityTrends
VALUES ('7/2/2014', 136, 378, 1421, 391, 9018, 2326)

1 个答案:

答案 0 :(得分:0)

我能够使用动态SQL和旋转来完成它,但它很长,但它足以满足我的要求。如果有人能给出建议来优化它,那将是热情的。 This article helped me.

以下是对我有用的查询:

    IF OBJECT_ID('tempdb..#PriorityData') IS NOT NULL DROP TABLE #PriorityData 

create table #PriorityData(
  CountDate date,
  [Priority 1] int,
  [Priority 2] int,
  [Priority 3] int,
  [Priority 4] int,
  [Priority 5] int,
  Totals int
  )

insert into #PriorityData
select * from (select top 7 * from PriorityTrends p 
               order by p.CountDate desc) as temp
order by temp.CountDate

select * from #PriorityData

DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX)

SET @columns = N''

SELECT @columns += N', ' + QUOTENAME(CountDate)
  FROM (SELECT p.CountDate FROM #PriorityData AS p) AS x

SET @sql = N'
SELECT ''Priority 1'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Priority 1] from #PriorityData
) AS j
PIVOT
(
  SUM([Priority 1]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', p1.[', ',['), 1, 1, '')
  + ')
) AS p1
UNION ALL
SELECT ''Priority 2'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Priority 2] from #PriorityData
) AS j
PIVOT
(
  SUM([Priority 2]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', p2.[', ',['), 1, 1, '')
  + ')
) AS p2
UNION ALL
SELECT ''Priority 3'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Priority 3] from #PriorityData
) AS j
PIVOT
(
  SUM([Priority 3]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', p3.[', ',['), 1, 1, '')
  + ')
) AS p3
UNION ALL
SELECT ''Priority 4'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Priority 4] from #PriorityData
) AS j
PIVOT
(
  SUM([Priority 4]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', p4.[', ',['), 1, 1, '')
  + ')
) AS p4
UNION ALL
SELECT ''Priority 5'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Priority 5] from #PriorityData
) AS j
PIVOT
(
  SUM([Priority 5]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', p5.[', ',['), 1, 1, '')
  + ')
) AS p5
UNION ALL
SELECT ''Totals'' AS Priority, ' + STUFF(@columns, 1, 2, '') + '
FROM
(
  SELECT CountDate, [Totals] from #PriorityData
) AS j
PIVOT
(
  SUM([Totals]) FOR CountDate IN ('
  + STUFF(REPLACE(@columns, ', t.[', ',['), 1, 1, '')
  + ')
) AS t'
PRINT @sql
EXEC sp_executesql @sql