查询将行拆分为两行

时间:2014-09-04 10:27:17

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

我需要根据查询中使用的表中两列的可为空性将行拆分为两行。

Sample Data
-------------------------------------------------------------------------------------------------
PrimaryID      SecondaryID      PassedSubject      FailedSubject     PSField       FSField
-------------------------------------------------------------------------------------------------
   1               11                ABC                XYZ          PSFldData1    FSFldData1
   1               12                DEF                NULL         PSFldData2    FSFldData2
   2               21                NULL               GHI          PSFldData3    FSFldData3

因此,对于上述数据,我正在寻找以下格式的可能结果,要求是如果字段" PassedSubject"和" FailedSubject"列是NOT NULL然后我需要将行拆分为,并且只应为包含PassedSubject值的行填充PSField(PassedSubject字段),并且应该为包含FailedSubject值的行填充FSField。

Sample Result
-------------------------------------------------------------------------------------------------
PrimaryID      SecondaryID      PassedSubject      FailedSubject     PSField       FSField
-------------------------------------------------------------------------------------------------
    1              11                ABC               NULL          PSFldData1    NULL
    1              11                NULL              XYZ           NULL          FSFldData1

我需要为#34; PassedSubject"" PassedSubject"和" FailedSubject"列。

3 个答案:

答案 0 :(得分:0)

;WITH PassedCTE AS
(
    SELECT PrimaryID,
           SecondaryID,
           PassedSubject,
           NULL AS FailedSubject,
           PSField,
           NULL AS FSField
    FROM   TableName
    WHERE  PassedSubject IS NOT NULL
),FailedCTE AS
(
    SELECT PrimaryID,
           SecondaryID,
           NULL AS PassedSubject,
           FailedSubject,
           NULL AS PSField,
           FSField
    FROM   TableName
    WHERE  FailedSubject IS NOT NULL
)
SELECT * 
FROM   PassedCTE 
UNION 
SELECT * 
FROM   FailedCTE 

答案 1 :(得分:0)

不确定您的示例结果是否与您想要的详细信息相对应(没有上面的行被分成两部分,因为secondaryId = 11都没有,但无论如何你要做什么(将一行分成两行)可以可以使用UNION ALL子句轻松实现,使用WHERE的不同标准:

SELECT PrimaryId, SecondaryID, ....
FROM table
WHERE <condition for first line to appear>
UNION ALL
SELECT PrimaryId, SecondaryID, ....
FROM table
WHERE <condition for second line to appear>

希望这有帮助

答案 2 :(得分:0)

SELECT PrimaryID,
       SecondaryID,
       CR.PassedSubject,
       CR.FailedSubject,
       CR.PSField,
       CR.FSField
FROM   TableName
CROSS APPLY (VALUES
     ( PassedSubject,NULL,PSField,NULL),
     ( NULL, FailedSubject,NULL,FSField))    
  CR ( PassedSubject, FailedSubject, PSField, FSField)
WHERE TableName.PassedSubject IS NOT NULL AND TableName.FailedSubject IS NOT NULL 
UNION ALL
SELECT * FROM TableName
WHERE TableName.PassedSubject IS NULL OR TableName.FailedSubject IS  NULL 

SQLFiddle

PRIMARYID   SECONDARYID PASSEDSUBJECT   FAILEDSUBJECT      PSFIELD     FSFIELD
        1            11           ABC        (null)     PSFldData1      (null)
        1            11        (null)           XYZ         (null)  FSFldData1
        1            12           DEF        (null)     PSFldData2  FSFldData2
        2            21        (null)           GHI     PSFldData3  FSFldData3