SQL转置数据

时间:2018-01-08 07:04:24

标签: sql-server tsql pivot

我有一个数据集如下:

line = ["RCI-AMG-3664 Laboratory Specialist","RCI-VBC-09568 Administrative Assistant in Cary, NC"]

数据集告诉您,对于某些HID HDate Comments 60038A 1/5/2018 5:39:48 TEST 60038A 1/6/2018 9:02:03 sample 60038A 1/8/2018 9:23:27 another test 60038A 1/6/2018 10:33:00 delayed 列,日期明确的注释会被保存 现在我想要的是,日期列应显示为具有各自注释的列。

我编写了与以下相同的数据透视查询:

ID

但是上面的查询给我的结果包含多个具有相同DECLARE @QUERY NVARCHAR(MAX), @Soucecolumn VARCHAR(MAX) SET @Soucecolumn = STUFF((SELECT distinct ',[' + Convert(varchar,[HDate],120) + ']' FROM TRCK_Comments FOR XML PATH('')),1,1,'') SET @QUERY = 'SELECT HID, ' + @Soucecolumn + ' FROM TRCK_Comments PIVOT (MAX(Comments) FOR [HDate] IN (' + @Soucecolumn + ')) AS pvt' exec sp_executesql @QUERY ID值的行 我希望单行与NULL相同,但该特定日期列中的可用注释

ID

预期的Reult

Actual result that i am getting

    HID 2018-01-04 11:26:05    2018-01-04 11:26:16  2018-01-04 11:26:27 2018-01-04 18:26:46
    1   this is sample comment  NULL                 NULL               NULL
    1   NULL                    this is comment      NULL               NULL
    2   NULL                    NULL                this is sample      NULL
    2   NULL                    NULL                NULL               nfdjfdfddf

这是我的实际数据集

    HID 2018-01-04 11:26:05    2018-01-04 11:26:16  2018-01-04 11:26:27 2018-01-04 18:26:46
    1   this is sample comment  this is comment     NULL                NULL

    2   NULL                    NULL                this is sample      nfdjfdfddf

想要通过HID进行分组(不是身份类型的ID)

1 个答案:

答案 0 :(得分:1)

使用您提供的示例数据:

CREATE TABLE #DataSource
(
    [ID] TINYINT 
   ,[HID] VARCHAR(12)
   ,[HDate] DATETIME2
   ,[Comments] VARCHAR(128)
);

INSERT INTO #DataSource ([ID], [HID], [HDate], [Comments])
VALUES ('1', '1', '2018-01-04 11:26:05', 'this is sample comment')
      ,('2', '1', '2018-01-04 11:26:16', 'this is sample comment this is sample comment')
      ,('3', '2', '2018-01-04 11:26:27', 'this is sample comment this is sample comment this is sample comment')
      ,('4', '2', '2018-01-04 18:26:46', 'nfdjfdfddf');

DECLARE @DynammicTSQLStatement NVARCHAR(MAX)
       ,@DynamicPIVOTColumns NVARCHAR(MAX);



SET @DynamicPIVOTColumns = STUFF
                          (
                                (
                                    SELECT ',[' + CONVERT(VARCHAR(19), [HDate], 120) + ']'
                                    FROM #DataSource
                                    GROUP BY [HDate]
                                    ORDER BY [HDate]
                                    FOR XML PATH('') ,TYPE
                                ).value('.', 'NVARCHAR(MAX)')
                                ,1
                                ,1
                                ,''
                          );


SET @DynammicTSQLStatement = N'
SELECT *
FROM
(
    SELECT [HID], [HDate], [Comments]
    FROM #DataSource
) DS
PIVOT
(
    MAX([Comments]) FOR [HDate] IN (' + @DynamicPIVOTColumns + ')
) PVT';

EXEC sp_executesql @DynammicTSQLStatement;

DROP TABLE #DataSource;

enter image description here