假设我有这张表:
Create table test(a int, b int, c int, d int)
我可以简单地写'select * from test'来获得以下第一条记录:
A B C D 1 2 3 4
但相反,我想要这样(单行录制四行):
A: 1 B: 2 C: 3 D: 4
有人可以帮我这么做吗?
这是查看远程桌面系统上的单个记录所必需的,该记录非常慢,并且在其上水平滚动很糟糕,并且其中有800列。所以我需要从单个记录中看到数据的格式。
答案 0 :(得分:11)
您可以使用UNPIVOT
函数执行此操作,下面的版本将列名和值连接在一起,但您始终可以将它们显示为单独的列:
select col+':'+cast(value as varchar(10)) col
from test
unpivot
(
value
for col in (A, B, C, D)
) unpiv
如果您有已知数量的列,则上述工作非常有用,但如果您要转换800列,则可能需要使用动态sql来执行此操作:
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
set @query
= 'select col+'':''+cast(value as varchar(10)) col
from test
unpivot
(
value
for col in ('+ @colsunpivot +')
) u'
exec(@query)
注意:使用UNPIVOT
时,需要转换的所有列的数据类型必须相同。因此,您可能需要根据需要转换/转换数据。
编辑#1 ,因为您的所有列上的数据类型都不同,并且您需要取消它们,所以您可以使用以下代码。
第一部分获取您要动态展开的列列表:
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
第二部分获取相同的列列表,但将cast
中的每一列包装为varchar
:
select @colsUnpivotCast = stuff((select ', cast('+quotename(C.name)+' as varchar(50)) as '+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
然后您的最终查询将是:
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@colsUnpivotCast AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
select @colsUnpivotCast = stuff((select ', cast('+quotename(C.name)+' as varchar(50)) as '+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
set @query
= 'select col+'':''+value col
from
(
select '+@colsUnpivotCast+'
from test
) src
unpivot
(
value
for col in ('+ @colsunpivot +')
) u'
exec(@query)
UNPIVOT
函数执行与UNION ALL
相同的过程,如下所示:
select col+':'+value as col
from
(
select A value, 'A' col
from test
union all
select cast(B as varchar(10)) value, 'B' col
from test
union all
select cast(C as varchar(10)) value, 'C' col
from test
union all
select cast(D as varchar(10)) value, 'D' col
from test
) src
所有查询的结果都是相同的:
| COL |
----------
| A:1 |
| B:2.00 |
| C:3 |
| D:4 |
编辑#2:使用UNPIVOT
删除任何可能导致某些数据丢失的空列。如果是这种情况,那么您需要使用IsNull()
包裹列来替换null
值:
DECLARE @colsUnpivot AS NVARCHAR(MAX),
@colsUnpivotCast AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
select @colsUnpivotCast = stuff((select ', IsNull(cast('+quotename(C.name)+' as varchar(50)), '''') as '+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('test')
for xml path('')), 1, 1, '')
set @query
= 'select col+'':''+value col
from
(
select '+@colsUnpivotCast+'
from test
) src
unpivot
(
value
for col in ('+ @colsunpivot +')
) u'
exec(@query)
替换空值,将得到如下结果:
| COL |
----------
| A:1 |
| B:2.00 |
| C: |
| D:4 |