从多个case语句中返回1行

时间:2016-06-15 17:51:59

标签: sql-server case dynamic-columns outer-apply

在下面带有case语句的端口中的查询中,我试图弄清楚当SegmentType不匹配时如何不返回空值。

QRSubscriberTag,MLKSubscriberTag,QTMSubscriber标签,实际上是由其SegID唯一标识的标签表和与SegID关联的segmentType。我使用带有子查询的外部应用,该子查询调用FOR XML PATH('')将标签连接在一起与特定标签相关联。

使用此方法的缺陷是,当标签不是QRSubscriberTag时,则会创建QRSubscriberTag列中的空记录。

QRSubscriberTag|QR01|MLKSubscriberTag|MLK01|QTMSubscriberTag|QTM01|QTM02
NULL           |NULL|NULL            |{Data}|null           |FH   |45
TKP ;OCP ;OCR ;|R   |NULL            |null |null            |null |null

我希望看到的是1行中的所有数据

QRSubscriberTag|QR01|MLKSubscriberTag|MLK01|QTMSubscriberTag|QTM01|QTM02
TKP ;OCP ;OCR ;|R   |NULL            |{Data}|null            |FH  |45

以下是我的代码

SET NOCOUNT ON

USE [Apps]

SELECT [BusRuleEnforceID], [FormatType], [Format],
        [tag], [TagValue], Operator,
       [QRSubscriberTag],[QR01],
       [MLKSubscriberTag],[MLK01], 
       [QTMSubscriberTag],[QTM01],[QTM02]
FROM
(
    SELECT 
        DISTINCT main.BusRuleEnforceID, 
        main.FormatType, 
        main.Format, 
        main.tag,
        main.TagValue,
        main.Operator,
        CASE
            WHEN seg.SegmentType = 'QR' THEN LEFT(SegsubscriberTags.list, LEN(SegsubscriberTags.list))
        END AS [QRSubscriberTag],
        CASE
            WHEN seg.SegmentType = 'MLK' THEN LEFT(SegsubscriberTags.list, LEN(SegsubscriberTags.list)-1)
        END AS [MLKSubscriberTag],
        CASE
            WHEN seg.SegmentType = 'QTM' THEN LEFT(SegsubscriberTags.list, LEN(SegsubscriberTags.list)-1)
        END AS [QTMSubscriberTag],
        c.ItemName as [SegmentId],
        Case
            WHEN c.ItemValue is not null and len(rtrim(c.ItemValue)) > 0 THEN c.ItemValue
            ELSE LEFT(TagOptions.list, LEN(TagOptions.list)-1)
        END as [Tags]
    FROM
        BMT_BusRulemaineria main 
    LEFT OUTER JOIN
        BMT_BusRuleServiceType st
    ON st.MappingmaineriaID = main.BusRuleEnforceID
    LEFT OUTER JOIN
        BMT_BusRuleSegment seg
    ON seg.BusRuleEnforceID = main.BusRuleEnforceID
    LEFT OUTER JOIN
        BMT_BusRuleItem c
    ON c.BusRuleEnforceSegID = seg.BusRuleEnforceSegID
    LEFT OUTER JOIN
        BMT_BusRuleItemOption co
    ON c.BusRuleItemId = co.BusRuleItemId
    OUTER APPLY 
    ( 
            SELECT 
            IsNull(co2.Tag, '{Unknown}') + ISNULL('=' + co2.TagValue, '') + ':' + IsNull(co2.ItemValue, '')+ ';' AS [text()] 
            FROM 
                BMT_BusRuleItemOption co2 
            WHERE 
                co.BusRuleItemId = co2.BusRuleItemId
            ORDER BY 
                co2.BusRuleItemId 
            FOR XML PATH('') 
        ) TagOptions (list) 
    OUTER APPLY 
    (
            SELECT 
            IsNull(porttag.Tag, '{Unknown}') + ' ' + ISNULL(porttag.Operator, '') + ' ' + IsNull(porttag.TagValue, '') + ';' AS [text()]
            FROM 
                BMT_BusRuleSegmentTag porttag 
            WHERE 
                seg.BusRuleEnforceSegID = porttag.BusRuleEnforceSegID
            ORDER BY 
                porttag.BusRuleEnforceSegID 
            FOR XML PATH('')
    ) SegsubscriberTags (list) 
        WHERE main.BusRuleEnforceID = 632563
    AND main.TagTypeId = 1
) AS QRSourceTable
PIVOT
(
    max([Tags])
    FOR [SegmentId] IN ([QR01], [MLK01], [QTM01], [QTM02])
) AS QRPivotTable
ORDER BY 
    [TagTypeId], [BusRuleEnforceID]

编辑:完整结果集 注意:QRSubscriber,MLKSubscriber和QTMSubscriber标签都有唯一的SegID。



<table><tbody><tr><th>FormatType</th><th>Format</th><th>Version</th><th>AmtCode</th><th>Date</th><th>LocationIdentifier</th><th>TagType</th><th>tag</th><th>TagValue</th><th>Operator</th><th>InfoCode</th><th>QRSubscriberTag</th><th>QR01</th><th>QR02</th><th>QR03</th><th>QR04</th><th>QR05</th><th>QR06</th><th>QR07</th><th>MLKSubscriberTag</th><th>MLK01</th><th>QTMSubscriberTag</th><th>QTM01</th><th>QTM02</th></tr><tr><td>DN</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>TIR</td><td>RPSAMN</td><td>NULL</td><td>exists</td><td>78</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>{Data}</td><td>NULL</td><td>TRH</td><td>675</td></tr><tr><td>DN</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>TIR</td><td>RPSAMN</td><td>NULL</td><td>exists</td><td>78</td><td>TKRARAP not exists TKRALAP not exists TKRADAP not exists </td><td>J</td><td>SKU</td><td>34</td><td>NULL</td><td>NULL</td><td>SILMULTU:2;SILMHLTU:67</td><td>TOPALD:{Data};QORITK{Data}</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td><td>NULL</td></tr></tbody></table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

当比较不匹配时,使用ELSE子句指定默认值,如下所示:

CASE WHEN port.SegmentType = 'QR' THEN 
  LEFT(SegDependantTags.list, LEN(SegDependantTags.list))
ELSE 'Your Value Rather Than NULL Goes Here'
END AS [QRSubscriberTag],

答案 1 :(得分:0)

您的Distinct子查询似乎不是在QRSourceTable子查询中使用Group By,而是在订阅者标记字段中使用MAXSELECT main.BusRuleEnforceID, main.FormatType, main.Format, main.tag, main.TagValue, main.Operator, max(CASE WHEN port.SegmentType = 'QR' THEN LEFT(SegDependantTags.list, LEN(SegDependantTags.list)) END) AS [QRSubscriberTag], max(CASE WHEN port.SegmentType = 'MLK' THEN LEFT(SegDependantTags.list, LEN(SegDependantTags.list)-1) END) AS [MLKSubscriberTag], max(CASE WHEN port.SegmentType = 'QTM' THEN LEFT(SegDependantTags.list, LEN(SegDependantTags.list)-1) END) AS [QTMSubscriberTag], c.ItemName as [SegmentId], Case WHEN c.ItemValue is not null and len(rtrim(c.ItemValue)) > 0 THEN c.ItemValue ELSE LEFT(TagOptions.list, LEN(TagOptions.list)-1) END as [Tags] from ... WHERE main.BusRuleEnforceID = 632563 AND main.TagTypeId = 1 GROUP BY main.BusRuleEnforceID, main.FormatType, main.Format, main.tag, main.TagValue, main.Operator 聚合函数:< / p>

width:100%