How to convert Column header to Row for loannumber(如何将列标题转换为贷款编号的行)
问题描述
我陷入了逆向旋转.我在下面有一个像#temp 这样的表.使用 sql server 2008 r2
I am stuck in unpivoting. I have a table like #temp below. Using sql server 2008 r2
Select LoanNumber = 2000424385
,[AmntType1] = 120.32
,[AmntType2] = 131.52
,[AmntType3] = 142.36
into #temp
从 #temp 中选择 *
select * from #temp
上面的表格只有一行,我想要下面的三行
Above table has only one row and i want three rows as below
LoanNumber Amount AmountType
2000424385 120.32 AmntType1
2000424385 131.52 AmntType2
2000424385 120.32 AmntType1
推荐答案
您应该能够通过 UNPIVOT 函数使用以下内容:
You should be able to use the following with the UNPIVOT function:
select loanNumber,
amount,
amounttype
from #temp
unpivot
(
amount
for amounttype in (AmntType1, AmntType2, AmntType3)
) unp;
请参阅SQL Fiddle with Demo.
或者因为您使用的是 SQL Server 2008 R2,这也可以使用 CROSS APPLY 编写:
Or because you are using SQL Server 2008 R2, this can also be written using CROSS APPLY:
select loannumber,
amount,
amounttype
from #temp
cross apply
(
values
('AmntType1', AmntType1),
('AmntType2', AmntType2),
('AmntType3', AmntType3)
) c (amounttype, amount);
参见SQL Fiddle with Demo
这篇关于如何将列标题转换为贷款编号的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将列标题转换为贷款编号的行
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
