T-SQL |动态转置

时间:2015-01-14 23:22:52

标签: sql sql-server tsql sql-server-2012

我有以下情况。

CREATE TABLE #Temp1  (ID INT , Summary varchar(50))
CREATE TABLE #Temp2  (ID INT , D_ID INT, Detail1 varchar(50), Detail2 varchar(50))

INSERT INTO #Temp1 (ID,Summary)
SELECT 1,'First'
UNION ALL
SELECT 2,'Second'
UNION ALL
SELECT 3,'Third'
UNION ALL
SELECT 4,'Four'

INSERT INTO #Temp2 (ID, D_ID, Detail1,Detail2)
SELECT 1,1,'One','_1_'
UNION ALL
SELECT 2,2,'Two','_2_'
UNION ALL
SELECT 2,3,NULL,'_3_'
UNION ALL
SELECT 3,4,'Three',NULL
UNION ALL
SELECT 3,5,NULL,NULL
UNION ALL
SELECT 3,6,'Three_2','_32_'


DROP TABLE #Temp1
DROP TABLE #Temp2

我正在寻找以下结果。

S_ID, S_Summary, D_Detail1_1, D_Detail1_2, D_Detail2_1, D_Detail2_2, D_Detail3_1, D_Detail3_2...)
1,    First,     One,         _1_          NULL         NULL         NULL         NULL...
2,    Second,    Two,         _2_          NULL         _3_          NULL         NULL...
3,    Third,     Three,       NULL         NULL         NULL         Three_2      _32_...
4,    Four,      NULL,        NULL         NULL         NULL         NULL         NULL...

摘要和详细信息表之间可能存在一对多关系,并且详细记录的数量可能会有所不同(动态)。

我不确定如何动态地进行PIVOT,并且仍能获得理想的结果。如果我没有弄错的话,PIVOT需要定义的行集来将其转置为列。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是使用Dynamic Crosstab完成的。您可以在此处阅读:http://www.sqlservercentral.com/articles/Crosstab/65048/

DECLARE @sql1 VARCHAR(2000) = ''
DECLARE @sql2 VARCHAR(2000) = ''
DECLARE @sql3 VARCHAR(2000) = ''

SELECT @sql1 = 
'SELECT
    t.ID AS S_ID
    , t.Summary AS S_Summary
'

SELECT @sql2 = @sql2 +
'   , MAX(CASE WHEN RN = ' + CONVERT(VARCHAR(10), RN) + ' THEN Detail1 END) AS D_Detail' + CONVERT(VARCHAR(10), RN) + '_1' + CHAR(10) +
'   , MAX(CASE WHEN RN = ' + CONVERT(VARCHAR(10), RN) + ' THEN Detail2 END) AS D_Detail' + CONVERT(VARCHAR(10), RN) + '_2' + CHAR(10)
FROM(
    SELECT DISTINCT ROW_NUMBER() OVER(PARTITION BY ID ORDER BY D_ID) AS RN FROM #Temp2
)t
ORDER BY RN

SELECT @sql3 = 
'FROM (
    SELECT
        t1.ID,
        t1.Summary,
        t2.D_ID,
        t2.Detail1,
        t2.Detail2,
        RN = ROW_NUMBER() OVER(PARTITION BY t1.ID ORDER BY D_ID)
    FROM #Temp1 t1
    LEFT JOIN #Temp2 t2
        ON t2.ID = t1.ID
) t
GROUP BY t.ID, t.Summary
ORDER BY t.ID'