使用SQL将单行拆分为多行

时间:2017-09-05 20:58:50

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

我正在尝试从单行生成多行,并且能够使用以下SQL执行此操作。但我想知道是否有更好的方法来编写这个SQL,因为它很长,不能应用于实时场景。谢谢。

注意: - 我的查询将返回所需的输出,但这不是一种有效的方法         解决问题。我的实际输入有更多列,我不想写'n'不。 SQL查询并为所有这些查询执行UNION ALL。 请建议更好的解决方案。谢谢。

Input :- 
 A.Col2 A.Col4 A.Col6 B.Col1 B.Col2 B.Col3 B.Col4 B.Col5 B.Col6 B.Col7
    300    301    302    100   9011    100   9002    100   9002    100
    300    301    302    101   8101     95   2001    100   2001    100
    300    301    302    102   8101    105   2001    110   2001    110

Desired output :-
100   300  9011  100
101   300  8101  95
102   300  8101  105
100   301  9002  100
101   301  2001  100
102   301  2001  110
100   302  9002  100
101   302  2001  100
102   302  2001  110

在我的示例中,我构建了包含总共10个字段的测试数据(前三个字段和其余七个字段。)对于第四列中的每个值,我必须构建三行,其中四列,三行看起来像: / p>

( 4th column, 1st column, 5th column, 6th column ),
( 4th column, 2nd column, 7th column, 8th column ),
( 4th column, 3rd column, 9th column, 10th column )

我当前的SQL:

SELECT B.Col1 AS Constructioncode,
       A.Col2 AS OccupancyCode,       
       B.Col2 AS MappingID,
       B.Col3 AS DamageFactor
  FROM 
( 
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) A
CROSS JOIN
(
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) B
WHERE B.Col1 > A.Col1
AND A.Col1 = 0
UNION ALL
SELECT B.Col1 AS Constructioncode,
       A.Col4 AS OccupancyCode,       
       B.Col4 AS MappingID,
       B.Col5 AS DamageFactor
  FROM 
( 
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) A
CROSS JOIN
(
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) B
WHERE B.Col1 > A.Col1
AND A.Col1 = 0 
UNION ALL
SELECT B.Col1 AS Constructioncode,
       A.Col6 AS OccupancyCode,       
       B.Col6 AS MappingID,
       B.Col7 AS DamageFactor
  FROM 
( 
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) A
CROSS JOIN
(
SELECT 0 AS Col1,300 AS Col2,0 AS Col3,301 AS Col4,0 AS Col5,302 AS Col6, 0 AS Col7 
UNION ALL
SELECT 100 AS Col1,9011 AS Col2,100 AS Col3,9002 AS Col4,100 AS Col5,9002 AS Col6,100 AS Col7 
UNION ALL
SELECT 101 AS Col1,8101 AS Col2,95 AS Col3,2001 AS Col4,100 AS Col5,2001 AS Col6,100 AS Col7
UNION ALL
SELECT 102 AS Col1,8101 AS Col2,105 AS Col3,2001 AS Col4,110 AS Col5,2001 AS Col6,110 AS Col7
) B
WHERE B.Col1 > A.Col1
AND A.Col1 = 0 

1 个答案:

答案 0 :(得分:4)

以下应该可以解决问题。我只通过一次数据就可以做到。

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    A_Col2 INT,
    A_Col4 INT,
    A_Col6 INT, 
    B_Col1 INT,
    B_Col2 INT,
    B_Col3 INT, 
    B_Col4 INT,
    B_Col5 INT,
    B_Col6 INT,
    B_Col7 INT 
    );
INSERT #TestData (A_Col2, A_Col4, A_Col6, B_Col1, B_Col2, B_Col3, B_Col4, B_Col5, B_Col6, B_Col7) VALUES
    (300, 301, 302, 100, 9011, 100, 9002, 100, 9002, 100),
    (300, 301, 302, 101, 8101,  95, 2001, 100, 2001, 100),
    (300, 301, 302, 102, 8101, 105, 2001, 110, 2001, 110);

--====================================================================

SELECT 
    Constructioncode = td.B_Col1,
    ab.OccupancyCode, 
    ab.MappingID, 
    ab.DamageFactor
FROM
    #TestData td
    CROSS APPLY ( VALUES 
                        (td.A_Col2, td.B_Col2, td.B_Col3), 
                        (td.A_Col4, td.B_Col4, td.B_Col5), 
                        (td.A_Col6, td.B_Col6, td.B_Col7) 
                    ) ab (OccupancyCode, MappingID, DamageFactor)
ORDER BY
    ab.OccupancyCode,
    td.B_Col1;

结果...

Constructioncode OccupancyCode MappingID   DamageFactor
---------------- ------------- ----------- ------------
100              300           9011        100
101              300           8101        95
102              300           8101        105
100              301           9002        100
101              301           2001        100
102              301           2001        110
100              302           9002        100
101              302           2001        100
102              302           2001        110