DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(ACT_DESC)
FROM (SELECT DISTINCT ACT_DESC FROM tpc) AS desc
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT MDCODE, ' + @ColumnName + '
FROM tpc
PIVOT(MAX(ACTUAL_DATE)
FOR tpcIN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery
此查询显示:
MDCODE | sample1 | sample2
--------------------------
123 | 1/2014 |
123 | | 2/2014
123 | | 3/2014
我想要的是这样的:
MDCODE | sample1 | sample2
--------------------------
123 | 1/2014 | 2/2014,3/2014
有谁知道如何连接列数据?有小费吗?
这是我获取数据的表:
mdcode | act_desc | actual_date
--------------------------
1234 | sample1 | 1/2014
1234 | sample2 | 2/2014
1234 | sample2 | 3/2014
实际日期是日期时间