4

Could someone help me convert the following on mssql please?

 ID   |  PROPERTY_NAME  | PROPERTY_VALUE
 1    |      name1      |     value
 1    |      name2      |     value
 1    |      name3      |     value
 2    |      name4      |     value
 2    |      name2      |     value
 3    |      name6      |     value
..

PROPERTY_NAME & PROPERTY_VALUE being the headers, id having multiple 'properties'

I want to convert it to:

ID  |  NAME1  | NAME2  | NAME3  | NAME4  | NAME5  | NAME6  | nameETC...
1   |  value  | value  | value  |        |        |        | valueETC...
2   |         | value  |        | value  |        |        | valueETC...
3   |         |        |        |        |        | value  | valueETC...
..

Where NAME1 | NAME2 | NAME3 etc are now the column headers.

I'm guessing a pivot and a 'select distinct PROPERTY_NAME from MycoolTable' but can't seem to put the two together.

This is as far as i got: (no server_id) column and all nulls everywhere) Clearly i'm stupid :D

DECLARE @cols AS NVARCHAR(MAX), @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct 
           ',' + QUOTENAME(PROPERTY_NAME)
               FROM [BSARA_DW_DB].[dbo].[SERVER_PROPERTY]
               FOR XML PATH(''), TYPE
               ).value('.', 'NVARCHAR(MAX)') 
               ,1,1,'');

SET @query = 'SELECT  '+ @cols + ' from 
         (
            SELECT SERVER_ID, PROPERTY_NAME, PROPERTY_CHAR_VAL
            FROM [BSARA_DW_DB].[dbo].[SERVER_PROPERTY]
        ) x
        pivot 
        (
            MAX(SERVER_ID)
            for PROPERTY_CHAR_VAL in (' + @cols + ')
        ) p ';

execute(@query)

Many thanks, Mike

4

1 回答 1

6

您当前查询的问题在于以下行:

MAX(SERVER_ID)

您想改为显示PROPERTY_CHAR_VALfor each PROPERTY_NAME。将SERVER_ID作为列的最终结果的一部分。

有时,当您使用 PIVOT 时,首先使用硬编码的值编写代码更容易,类似于:

select id, name1, name2, name3, name4
from
(
  select id, property_name, property_value
  from yourtable
) d
pivot
(
  max(property_value)
  for property_name in (name1, name2, name3, name4)
) piv;

请参阅SQL Fiddle with Demo

一旦你有了一个具有正确逻辑的版本,你就可以将它转换为动态 SQL 以获得结果。这将创建一个将执行的 sql 字符串,它将包含所有新列的名称。

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(PROPERTY_NAME) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT id, ' + @cols + ' 
            from 
            (
              select id, property_name, property_value
              from yourtable
            ) x
            pivot 
            (
                max(property_value)
                for property_name in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo。两者都会给出结果:

| ID |  NAME1 |  NAME2 |  NAME3 |  NAME4 |  NAME6 |
|----|--------|--------|--------|--------|--------|
|  1 |  value |  value |  value | (null) | (null) |
|  2 | (null) |  value | (null) |  value | (null) |
|  3 | (null) | (null) | (null) | (null) |  value |
于 2013-10-30T16:55:01.333 回答