简体   繁体   English

SQL查询,用于获取格式为“列标题:列值”的单个记录

[英]SQL query for fetching a single record in format “column heading: column value”

Suppose I have this table: 假设我有这张桌子:

Create table test(a int, b int, c int, d int)

I can write simply 'select * from test' to get the following first record: 我可以简单地编写“从测试中选择*”来获得以下第一条记录:

A    B    C    D  
1    2    3    4

But instead, I want it like this (Four rows for single record): 但是,我希望这样(单个记录四行):

A: 1  
B: 2  
C: 3  
D: 4

Can someone help me in doing this? 有人可以帮我吗?

This is required to view a single record on remote desktop system which is extremely slow, and horizontal scrolling on it sucks, and has 800 columns in it. 要在远程桌面系统上查看单条记录非常慢,这非常慢,并且水平滚动很烂,并且有800列,这是必需的。 So I need to see the format of data from a single record. 因此,我需要从单个记录中查看数据的格式。

You can use the UNPIVOT function to do this, the version below concatenates the column name and value together, but you can always display them as separate columns: 您可以使用UNPIVOT函数执行此操作,以下版本将列名和值连接在一起,但是您始终可以将它们显示为单独的列:

select col+':'+cast(value as varchar(10)) col
from test
unpivot
(
  value
  for col in (A, B, C, D)
) unpiv

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

The above works great if you have a known number of columns, but if you have 800 columns that you want to transform, you might want to use dynamic sql to perform this: 如果您具有已知的列数,则以上方法非常有用,但是如果您要转换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)

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Note: when using UNPIVOT the datatypes of all of the columns that need to be transformed must be the same. 注意:使用UNPIVOT时,需要转换的所有列的数据类型必须相同。 So you might have to cast/convert data as needed. 因此,您可能需要根据需要强制转换/转换数据。

Edit #1 , since your datatypes are different on all of your columns and you need to unpivot them, then you can use the following code. 编辑#1 ,因为所有列的数据类型都不同,并且您需要取消透视它们,那么可以使用以下代码。

The first piece get the list of columns that you want to unpivot dynamically: 第一部分获取要动态取消透视的列的列表:

select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')

The second piece gets the same list of columns but wraps each column in a cast as a varchar : 第二件得到列的列表相同,但在包装的每一列castvarchar

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, '')

Then your final query will be: 然后,您的最终查询将是:

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)

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

The UNPIVOT function is performing the same process as a UNION ALL which would look like this: 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

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

The result of all of the queries is the same: 所有查询的结果都相同:

|    COL |
----------
|    A:1 |
| B:2.00 |
|    C:3 |
|    D:4 |

Edit #2: using UNPIVOT strips out any of the null columns which could cause some data to drop. 编辑2:使用UNPIVOT删除任何可能导致某些数据UNPIVOT的空列。 If that is the case, then you will want to wrap the columns with IsNull() to replace the null values: 如果是这种情况,那么您将需要用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)

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

Replacing the null values, will give a result like this: 替换空值,将得到如下结果:

|    COL |
----------
|    A:1 |
| B:2.00 |
|     C: |
|    D:4 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM