我有下面的表格,但不确定是否有可能转动它并保留所有标签。TSQL枢轴多列
RATIO RESULT SCORE GRADE
Current Ratio 1.294 60 Good
Gearing Ratio 0.3384 70 Good
Performance Ratio 0.0427 50 Satisfactory
TOTAL NULL 180 Good
我会承认自己不是很好的使用枢轴,所以导致这个输出经过几次尝试:
SELECT 'RESULT' AS 'Ratio'
,[Current Ratio] AS 'Current Ratio'
,[Gearing Ratio] AS 'Gearing Ratio'
,[Performance Ratio] AS 'Performance Ratio'
,[TOTAL] AS 'TOTAL'
FROM
(
SELECT RATIO, RESULT
FROM GRAND_TOTALS
) AS SREC
PIVOT
(
MAX(RESULT)
FOR RATIO IN ([Current Ratio],[Gearing Ratio], [Performance Ratio], [TOTAL])
) AS PVT
这给出结果:
Ratio Current Ratio Gearing Ratio Performance Ratio
Result 1.294 0.3384 0.0427
我会承认感到非常难以接受下一步做什么以产生我需要的结果:
Ratio Current Ratio Gearing Ratio Performance Ratio TOTAL
Result 1.294 0.3384 0.0427 NULL
Score 60 70 50 180
Grade Good Good Satisfactory Good
你使用的是什么版本的sql server? – Taryn
[T-SQL中的Multiple Column Pivot]的可能的重复(http://stackoverflow.com/questions/947281/multiple-column-pivot-in-t-sql) –