我想将多行转换为SQL Server中的单个自定义行

时间:2017-01-05 04:54:47

标签: sql sql-server sql-server-2008

我想根据组将多行转换为单行。

我附上了样本截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

Nolan的答案更加通用,但是如果您只需将这些固定数量的行转换为静态列集,为什么不保持简单呢?

;WITH cteYourData
    (
    ColumnName,
    Value,
    NewValue,
    NV_Decode,
    OldValue,
    OV_Decode
    )
    AS
    (
    SELECT 'HearingDetailsId', 379785, 379785, NULL, NULL, NULL
    UNION ALL SELECT 'Informal', 379785, 5641, 'karthick.devaraj', 863, 'Prabhu'
    UNION ALL SELECT 'Formal', 379785, 5641, '5641', NULL, NULL
    )
    SELECT *
        INTO #YourData
        FROM cteYourData;

SELECT HearingDetailsId = hd.Value,
    [Informal NewValue] = inf.NewValue,
    [Informal NV_Decode] = inf.NV_Decode,
    [Informal OldValue] = inf.OldValue,
    [Informal OV_Decode] = inf.OV_Decode,
    [Formal NewValue] = frm.NewValue,
    [Formal NV_Decode] = frm.NV_Decode,
    [Formal OldValue] = frm.OldValue,
    [Formal OV_Decode] = frm.OV_Decode
    FROM #YourData AS hd
        LEFT OUTER JOIN #YourData AS inf
            ON hd.Value = inf.Value
            AND inf.ColumnName = 'Informal'
        LEFT OUTER JOIN #YourData AS frm
            ON hd.Value = frm.Value
            AND frm.ColumnName = 'Formal'
    WHERE hd.ColumnName = 'HearingDetailsId'

答案 1 :(得分:0)

CREATE TABLE #tt(ColumnName varchar(100),[value] int,NewValue int,NV_Decode varchar(100),OldVaue int,OV_Decode varchar(100))
 INSERT INTO #tt
 VALUES('HearingDetailsid',3797685,3797685,NULL,NULL,NULL )
 ,('Informal',3797685,5641,'karthick. devaral',863,'Prabhu' )
 ,('Formal',3797685,5641,'5641',null,null )

 SELECT * FROM (
        SELECT [value] AS [HearingDetailsid],  c.col_title,c.col_value FROM #tt AS t
        CROSS APPLY(VALUES (ColumnName+'NewValue',CONVERT(VARCHAR,NewValue)),(ColumnName+'NV_Decode',CONVERT(VARCHAR,NV_Decode))
                         ,(ColumnName+'OldVaue',CONVERT(VARCHAR,OldVaue)),(ColumnName+'OV_Decode',CONVERT(VARCHAR,OV_Decode))
        ) c(col_title,col_value)
        WHERE t.ColumnName!='HearingDetailsid'
) AS t 
PIVOT(MAX(t.col_value) FOR t.col_title IN (InformalNewValue,InformalNV_Decode,InformalOldVaue,InformalOV_Decode,FormalNewValue
 ,FormalNV_Decode,FormalOldVaue,FormalOV_Decode)) p

如果未修复列名,则可以使用动态脚本:

   DECLARE @col VARCHAR(max),@sql VARCHAR(max)
    SELECT @col=ISNULL(@col+',','')+  t.ColumnName+'NewValue,'+t.ColumnName+'NV_Decode,' + t.ColumnName+'OldVaue,'+t.ColumnName+'OV_Decode' 
    FROM #tt AS t WHERE t.value!=t.NewValue
    --PRINT @col

    SET @sql='SELECT * FROM (
                SELECT [value] AS [HearingDetailsid],  c.col_title,c.col_value FROM #tt AS t
                CROSS APPLY(VALUES (ColumnName+''NewValue'',CONVERT(VARCHAR,NewValue)),(ColumnName+''NV_Decode'',CONVERT(VARCHAR,NV_Decode))
                                 ,(ColumnName+''OldVaue'',CONVERT(VARCHAR,OldVaue)),(ColumnName+''OV_Decode'',CONVERT(VARCHAR,OV_Decode))
                ) c(col_title,col_value)
                WHERE t.value!=t.NewValue
        ) AS t 
        PIVOT(MAX(t.col_value) FOR t.col_title IN ('+@col+')) p'
    EXEC(@sql)
HearingDetailsid InformalNewValue               InformalNV_Decode              InformalOldVaue                InformalOV_Decode              FormalNewValue                 FormalNV_Decode                FormalOldVaue                  FormalOV_Decode
---------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------ ------------------------------
3797685          5641                           karthick. devaral              863                            Prabhu                         5641                           5641                           NULL                           NULL