1 回答
TA贡献1810条经验 获得超4个赞
由于要转动多列数据,我会首先建议unpivoting的result,score并且grade列,所以你不必多列,但你将有多个行。
根据您的SQL Server版本,您可以使用UNPIVOT函数或CROSS APPLY。取消数据透视的语法类似于:
select ratio, col, value
from GRAND_TOTALS
cross apply
(
select 'result', cast(result as varchar(10)) union all
select 'score', cast(score as varchar(10)) union all
select 'grade', grade
) c(col, value)
请参阅带有演示的SQL Fiddle。一旦数据被取消透视,就可以应用PIVOT功能:
select ratio = col,
[current ratio], [gearing ratio], [performance ratio], total
from
(
select ratio, col, value
from GRAND_TOTALS
cross apply
(
select 'result', cast(result as varchar(10)) union all
select 'score', cast(score as varchar(10)) union all
select 'grade', grade
) c(col, value)
) d
pivot
(
max(value)
for ratio in ([current ratio], [gearing ratio], [performance ratio], total)
) piv;
请参阅带有演示的SQL Fiddle。这将为您提供结果:
| RATIO | CURRENT RATIO | GEARING RATIO | PERFORMANCE RATIO | TOTAL |
|--------|---------------|---------------|-------------------|-----------|
| grade | Good | Good | Satisfactory | Good |
| result | 1.29400 | 0.33840 | 0.04270 | (null) |
| score | 60.00000 | 70.00000 | 50.00000 | 180.00000 |
- 1 回答
- 0 关注
- 644 浏览
添加回答
举报