TSQL PIVOT MULTIPLE COLUMNS(TSQL PIVOT 多列)
问题描述
我有下表,但不确定是否可以旋转它并保留所有标签.
I have the following table but unsure of whether it is possible to pivot this and retain all the labels.
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
我承认我不太擅长使用枢轴,所以经过多次尝试后得到了这个输出:
I will admit to not being very good with the use of pivots, so after several attempts resulting in this output:
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
我承认我对接下来要做什么才能产生我需要的结果感到非常困惑:
I will admit to be feeling very stumped on what to do next to produce the result I need which is:
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
推荐答案
既然要透视多列数据,我首先建议取消透视result
,score
和 grade
列,因此您没有多列,但会有多行.
Since you want to pivot multiple columns of data, I would first suggest unpivoting the result
, score
and grade
columns so you don't have multiple columns but you will have multiple rows.
根据您的 SQL Server 版本,您可以使用 UNPIVOT 函数或 CROSS APPLY.取消数据透视的语法类似于:
Depending on your version of SQL Server you can use the UNPIVOT function or CROSS APPLY. The syntax to unpivot the data will be similar to:
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 with Demo.数据取消透视后,您就可以应用 PIVOT 函数:
See SQL Fiddle with Demo. Once the data has been unpivoted, then you can apply the PIVOT function:
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 with Demo.这会给你结果:
See SQL Fiddle with Demo. This will give you the result:
| 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 |
这篇关于TSQL PIVOT 多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TSQL PIVOT 多列
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01