我必须处理以下表格结构,将其转换为类似下图中第二个表格的视图:
由于列" Var_Columns "可以包含一百多个不同的值,我想要一些动态语句,它们将使用这些值来创建具有相应值的列标题。
理想情况下,月份名称的格式应为" 2016-01-01"一月等等。
我希望我能提供一些试用和错误代码,但我已经陷入了困境。 因此,我创建了一个SQL Fiddle,以便您查看。
希望有人知道如何解决这个问题。
谢谢,
丹尼尔
答案 0 :(得分:1)
这是我的解决方案,使用动态sql来派生列名。
创建测试数据集:
CREATE TABLE input
(
[ID] int NOT NULL,
[Version] [smallint] NOT NULL,
[RowNo] [int] NOT NULL,
[Dept] [nvarchar](max) NULL,
[Info] [nvarchar](max) NULL,
[Year] [nvarchar](max) NULL,
[Var_Columns] [nvarchar](max) NULL,
[January] [nvarchar](max) NULL,
[Febuary] [nvarchar](max) NULL,
[March] [nvarchar](max) NULL,
[April] [nvarchar](max) NULL,
[May] [nvarchar](max) NULL,
[June] [nvarchar](max) NULL,
[July] [nvarchar](max) NULL,
[August] [nvarchar](max) NULL,
[September] [nvarchar](max) NULL,
[October] [nvarchar](max) NULL,
[November] [nvarchar](max) NULL,
[December] [nvarchar](max) NULL,
)
;
INSERT INTO input
([ID], [Version], [RowNo],[Dept], [Info],[Year],[Var_Columns],[January],[Febuary], [March],[April],[May],[June],[July]
,[August], [September] ,[October],[November],[December]
)
VALUES
(1,2,1,'DeptA','Sheet','2016','varCol1','1','2','3','4','5','6','7','8','9','10','11','12'),
(1,2,2,'DeptA','Sheet','2016','varCol2','11','22','33','44','55','66','77','88','99','100','110','120'),
(1,2,3,'DeptA','Sheet','2016','varCol3','111','222','333','444','555','666','777','888','999','101','111','122'),
(2,2,1,'DeptB','Sheet','2016','varCol1','10','20','30','40','50','60','70','80','9','10','11','12')
;
实际解决方案:
--Create a variable to hold the column names for the unpivot
DECLARE @col VARCHAR(MAX) = '';
-- insert the distinct Var_column entries into a temp table
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp;
SELECT DISTINCT [Var_Columns] INTO #temp
FROM input
--Assign the distinct Var_column entries in the temp table to the variable @col
SELECT @col += QUOTENAME([Var_Columns])+',' from #temp;
--generate the dynamic query
DECLARE @sql varchar(max) = ''
SELECT @sql = '
;WITH CTE AS (
SELECT *
FROM
(SELECT [ID], [Version], [Dept], [Info],[Year],[Var_Columns],[January],[Febuary], [March],[April],[May],[June],[July],[August],[September],[October],[November],[December]
FROM input) p
UNPIVOT
([Values] FOR [Month] IN
([January],[Febuary], [March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
)AS unpvt)
SELECT [ID], [Version], [Dept], [Info],[Year],[Month],
'+SUBSTRING(@col,1,LEN(@col)-1)+'
FROM
(SELECT *
FROM CTE) AS SourceTable
PIVOT
(
MAX([Values])
FOR Var_Columns IN ('+SUBSTRING(@col,1,LEN(@col)-1)+')
) AS PivotTable;
; '
-- prin the query and then execute.
print (@sql)
EXECUTE (@sql)
GO
结果: