这就是我们迄今为止所拥有的。
这是我们现在拥有的输入数据。
SCOREQuali_39 K_Sperr_Cod DB2_TOTAL
NULL 0 NULL
NULL 0 690.226684288121
NULL 5 NULL
NULL 13 -20.6408152008628
G 0 -54.580159556553
NULL 5 -5.37403744493121
H 0 3593.56170469699
NULL 5 NULL
G 0 NULL
F 0 NULL
G 0 -62.6926387195259
NULL 5 NULL
NULL 5 NULL
H 0 1408.52983029661
这是我们目前得到的 SQL 代码。
DECLARE @columns VARCHAR(MAX)
SELECT @columns = COALESCE (
@columns + ',[' + [SCOREQuali_39] + ']',
'[' + [SCOREQuali_39] + ']'
)
FROM [dbo].[KXEN]
GROUP BY [SCOREQuali_39]
ORDER BY [SCOREQuali_39]
DECLARE @query VARCHAR(8000)
SET @query = 'SELECT *
FROM (SELECT [K_Sperr_Cod],
[SCOREQuali_39],
[DB2_TOTAL]
FROM [dbo].[KXEN]
) PIV
PIVOT (
SUM([DB2_TOTAL]) FOR [SCOREQuali_39] in (' + @columns + ')
) AS chld'
EXECUTE (@query)
GO
这来自 SQL 代码的输出..
K_Sperr_Cod A E F G H Z
0 255068.499500406 330873.173845716 61751973.8684996 47726691.9141684 219108948.21594 5796194.75837109
1 NULL NULL 48104.4040577828 24257.7279835226 83930.9535365729 7166.90224131628
2 NULL NULL 4200.02651139712 186.299907511524 21404.4251675196 NULL
3 NULL NULL NULL NULL NULL NULL
4 NULL NULL 90.6788153645106 NULL NULL NULL
5 NULL NULL 3299.17171760959 6898.57843394471 34617.9499761113 141.569391059137
6 NULL NULL 8223.99222164943 NULL 36399.7235520784 NULL
7 NULL 9431.17125064557 49192.2298488292 27022.0434106792 259157.952646459 2613.38080876173
8 NULL NULL 5085.36984841527 4579.52341830547 45967.6453351029 NULL
9 NULL NULL 5016.61173948202 4684.68398880511 6507.85505033565 76.5867787484384
10 NULL NULL 32637.6131091879 16337.6524219655 156041.485722849 NULL
11 NULL NULL 1959.61759693503 NULL 175.003852737077 NULL
12 NULL NULL 49377.6994079961 9025.28132917931 67220.6009906211 213.921132670872
13 NULL NULL 2023.68914784688 1011.24171909977 3508.66494748362 NULL
14 NULL NULL 9894.59491921946 3660.72246455194 26979.7285260416 33.2429911696158
15 NULL NULL 68.5418213084175 NULL 13888.9894847372 NULL
16 NULL NULL 62.6439310312258 177.563416355756 147.875800320802 NULL
这个想法是能够使用多个列(现在只有 SCOREQuali_39)有什么想法吗?