我的[pivot]表如下。
A B C D E F G H I J K L M N O P
400.71 2.23 26.71 64847.63 0 0 0 0 0 161 0 31.29 0 0 2018 January
761.4 4.23 50.76 56724.12 0 0 0 0 0 74 0 29.9 0 0 2018 February
535.81 2.98 35.72 48323.59 0 0 0 0 0 90 0 42.09 0 0 2018 March
490.47 2.72 32.7 76690.51 0.77 14.72 0.81 1.59 247.93 156 0 36.84 0 0 2018 April
461.8 2.57 30.79 53348.36 1.58 1.86 0.15 1.58 183.05 115 0 40.11 0 0 2018 May
528.3 2.94 35.22 79223.21 0 0 0 0 0 149 0 38.38 0 0 2018 June
447.8 2.49 29.85 69752.76 1.77 3.09 0.25 3 466.9 155 0 42.57 0.01 0 2018 July
488.72 2.76 33.1 87675.79 4.29 11.94 0.34 5.29 949.58 179 0 40.09 0.01 0 2018 August
374.28 2.08 24.95 80357.47 1.65 5.01 0.25 6.97 1496.05 214 0 45.62 0.02 0 2018 September
我想按以下方式安排它:
答案 0 :(得分:3)
尝试一下
IF OBJECT_ID('dbo.TempData')IS NOT NULL
DROP TABLE TempData
;WITH CTE(A , B , C , D , E , F , G , H , I , J , K , L , M , N, O , P)
AS
(
SELECT 400.71 , 2.23 , 26.71 , 64847.63 , 0 , 0 , 0 , 0 , 0 , 161 , 0 , 31.29 , 0 , 0, 2018, 'January' UNION ALL
SELECT 761.4 , 4.23 , 50.76 , 56724.12 , 0 , 0 , 0 , 0 , 0 , 74 , 0 , 29.9 , 0 , 0, 2018, 'February' UNION ALL
SELECT 535.81 , 2.98 , 35.72 , 48323.59 , 0 , 0 , 0 , 0 , 0 , 90 , 0 , 42.09 , 0 , 0, 2018, 'March' UNION ALL
SELECT 490.47 , 2.72 , 32.7 , 76690.51 , 0.77 , 14.72 , 0.81 , 1.59 , 247.93 , 156 , 0 , 36.84 , 0 , 0, 2018, 'April' UNION ALL
SELECT 461.8 , 2.57 , 30.79 , 53348.36 , 1.58 , 1.86 , 0.15 , 1.58 , 183.05 , 115 , 0 , 40.11 , 0 , 0, 2018, 'May' UNION ALL
SELECT 528.3 , 2.94 , 35.22 , 79223.21 , 0 , 0 , 0 , 0 , 0 , 149 , 0 , 38.38 , 0 , 0, 2018, 'June' UNION ALL
SELECT 447.8 , 2.49 , 29.85 , 69752.76 , 1.77 , 3.09 , 0.25 , 3 , 466.9 , 155 , 0 , 42.57 , 0.01 , 0, 2018, 'July' UNION ALL
SELECT 488.72 , 2.76 , 33.1 , 87675.79 , 4.29 , 11.94 , 0.34 , 5.29 , 949.58 , 179 , 0 , 40.09 , 0.01 , 0, 2018, 'August' UNION ALL
SELECT 374.28 , 2.08 , 24.95 , 80357.47 , 1.65 , 5.01 , 0.25 , 6.97 , 1496.05, 214 , 0 , 45.62 , 0.02 , 0, 2018, 'September'
)
SELECT * INTO TempData FROM CTE
SELECT [Year],Category,
ISNULL([January] ,'0' ) AS [January]
,ISNULL([February] ,'0' ) AS [February]
,ISNULL([March] ,'0' ) AS [March]
,ISNULL([April] ,'0' ) AS [April]
,ISNULL([May] ,'0' ) AS [May]
,ISNULL([June] ,'0' ) AS [June]
,ISNULL([July] ,'0' ) AS [July]
,ISNULL([August] ,'0' ) AS [August]
,ISNULL([September],'0' ) AS [September]
,ISNULL([October] ,'0' ) AS [October]
,ISNULL([November] ,'0' ) AS [November]
,ISNULL([December] ,'0' ) AS [December]
FROM
(
SELECT O AS [Year],P AS Months,data,Category
FROM TempData
CROSS APPLY (VALUES(A ,'A' ),(B,'B'),(C,'C'), (D,'D'),(E,'E'),(F,'F'),(G,'G'),
(H,'H'),(I,'I'),(J,'J'),(K,'K'),(L,'L'),(M,'M'),(N,'N')
)Dt (data,Category)
)dt2
PIVOT
(
MAX(data) FOR Months IN ([January]
,[February]
,[March]
,[April]
,[May]
,[June]
,[July]
,[August]
,[September]
,[October]
,[November]
,[December]
)
)Pvt
动态Sql方法
IF OBJECT_ID('Tempdb..##Months')IS NOT NULL
DROP TABLE ##Months
SELECT number,
DATENAME(MONTH,DATEADD(MONTH ,number-DATEPART(MONTH,GETDATE()),GETDATE())) AS Months
INTO ##Months
FROM master.dbo.spt_values WHERE type='P'
AND number BETWEEN 1 AND 12 ORDER BY 1
DECLARE @Sql nvarchar(max) ,
@Sql2 nvarchar(max) ,
@Query nvarchar(max),
@ISNUllQuery nvarchar(max)
SELECT @Query= STUFF((SELECT ', '+QUOTENAME(Months) FROM ##Months ORDER BY ##Months.number FOR XML PATH ('')),1,1,'')
SELECT @ISNUllQuery =STUFF((SELECT ', '+'ISNULL('+QUOTENAME(Months)+',''0'') AS '+QUOTENAME(Months)
FROM ##Months ORDER BY ##Months.number FOR XML PATH ('')),1,1,'')
SELECT DISTINCT @Sql2= 'SELECT [Year],Category,'+@ISNUllQuery+'
FROM(
SELECT [O] AS [Year],[P] AS MonthInfo,CAST(Data AS VARCHAR(20)) AS Data,Category
FROM TempData CROSS APPLY (VALUES ' +STUFF((SELECT DISTINCT ', '+'('+(COLUMN_NAME) +','+''''+COLUMN_NAME+''''+')'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='TempData' AND COLUMN_NAME NOT IN ('o','p')FOR XML PATH ('')),1,1,'')
+ ' ) AS Dt (Data,Category) )dtb PIVOT(MAX(Data) FOR MonthInfo IN ('+@Query+')) AS PVT'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='TempData'
PRINT @Sql2
EXEC (@Sql2)
答案 1 :(得分:2)
您要取消枢轴旋转。我会这样做:
select t.o, v.category,
max(case when t.p = 'January' then v.val end) as jan,
max(case when t.p = 'February' then v.val end) as feb,
. . .
max(case when t.p = 'December' then v.val end) as dec
from t cross apply
(values ('a', a), ('b', b), . . . ('n', n)
) v(category, val)
group by t.o, t.p;