当第二个值为null时,有条件地加入或忽略结果

时间:2015-11-17 16:19:48

标签: sql sql-server reporting-services ssrs-2012

我试图专注于我的主要查询部分给我带来麻烦。此查询用于填充具有可选日期参数的SSRS报告。此查询代码段用于LEFT Join。如果未指定日期,则应返回所有匹配的记录。这似乎有效。但是,如果我指定日期并且在第二列中没有返回任何记录,则不应显示该行的结果。不确定我是否解释得那么好。

以下是摘录:

DECLARE @DispoStartDt datetime, @DispoEndDt datetime, @DispoCode varchar(800)
SET @DispoStartDt = '5-1-2050'
--SET @DispoEndDt = '12-31-2014'
SET @DispoCode = '_NULL'

SELECT  c1.CaseID,
        STUFF((SELECT '; ' + CONVERT(VARCHAR(10), ct.DispoDt, 101)
                FROM jw50_CountInvPers ct
                WHERE ct.CaseID = '00-100'
                AND     (ct.DispoDt >= DATEADD(dd, DATEDIFF(dd, 0, ISNULL(@DispoStartDt, ct.DispoDt)), 0) AND ct.DispoDt < DATEADD(dd, DATEDIFF(dd, -1, ISNULL(@DispoEndDt, ct.DispoDt)), 0))

                ORDER BY DispoDesc
                FOR XML PATH('')), 1, 1, '') [DispoDts]
FROM    jw50_Case c1
WHERE c1.CaseID = '00-100'
GROUP BY    c1.CaseID

我的结果是:

CaseID     DispoDts
 00-100      NULL

如果DispoDts为NULL,我不希望返回任何记录。由于我无法引用列别名,除了将其放入子选择之外还有其他方法吗?

修改

以下是我的加入中使用的代码段。由于左连接,我得到了带有NULL DispoDts的行。如果我指定一个开始日期,我希望将结果视为内连接,只返回与连接行匹配的结果。如果我没有指定日期,如果存在联接,请在主表中提供所有内容。

DECLARE @DispoStartDt datetime, @DispoEndDt datetime, @DispoCode varchar(800)
SET @DispoStartDt = '5-1-2015'
--SET @DispoEndDt = '12-31-2014'
SET @DispoCode = '_NULL'

SELECT    c.CaseID
        , c.ProsNum AS FileNum
        , c.CaseStatusDesc AS CaseStatus
        , disp.DispoDts
FROM    jw50_Case c
    LEFT JOIN   (SELECT CaseID, DispoDts
                 FROM   (
                            SELECT  c1.CaseID,
                                STUFF((SELECT '; ' + CONVERT(VARCHAR(10), ct.DispoDt, 101)
                                        FROM jw50_CountInvPers ct
                                        WHERE ct.CaseID = c1.CaseId
                                            AND     (ct.DispoDt >= DATEADD(dd, DATEDIFF(dd, 0, ISNULL(@DispoStartDt, ct.DispoDt)), 0) AND ct.DispoDt < DATEADD(dd, DATEDIFF(dd, -1, ISNULL(@DispoEndDt, ct.DispoDt)), 0))

                                    ORDER BY DispoDesc
                                    FOR XML PATH('')), 1, 1, '') [DispoDts]
                    FROM    jw50_Case c1
                    WHERE c1.CaseID = c1.CaseId
                    GROUP BY    c1.CaseID
                    ) a
             WHERE DispoDts IS NOT NULL
) disp ON disp.CaseID = c.CaseID
ORDER BY    CaseID, DispoDts

编辑2:

好的,感觉很脏,但确实有效。

WHERE (@DispoStartDt IS NULL AND @DispoEndDt IS NULL)
    OR (@DispoStartDt IS NOT NULL AND @DispoEndDt IS NOT NULL AND disp.DispoDts IS NOT NULL)
    OR (@DispoStartDt IS NOT NULL AND @DispoEndDt IS NULL AND disp.DispoDts IS NOT NULL)
    OR (@DispoStartDt IS NULL AND @DispoEndDt IS NOT NULL AND disp.DispoDts IS NOT NULL)

1 个答案:

答案 0 :(得分:0)

您可以尝试:

DECLARE @DispoStartDt datetime, @DispoEndDt datetime, @DispoCode varchar(800)
SET @DispoStartDt = '5-1-2050'
--SET @DispoEndDt = '12-31-2014'
SET @DispoCode = '_NULL'

SELECT CaseID,
        [DispoDts]
FROM (
    SELECT  c1.CaseID,
            STUFF((SELECT '; ' + CONVERT(VARCHAR(10), ct.DispoDt, 101)
                    FROM jw50_CountInvPers ct
                    WHERE ct.CaseID = '00-100'
                    AND     (ct.DispoDt >= DATEADD(dd, DATEDIFF(dd, 0, ISNULL(@DispoStartDt, ct.DispoDt)), 0) AND ct.DispoDt < DATEADD(dd, DATEDIFF(dd, -1, ISNULL(@DispoEndDt, ct.DispoDt)), 0))

                    ORDER BY DispoDesc
                    FOR XML PATH('')), 1, 1, '') [DispoDts]
    FROM    jw50_Case c1
    WHERE c1.CaseID = '00-100'
    GROUP BY    c1.CaseID
) as tbaux
WHERE [DispoDts] IS NOT NULL

这样您获得的值与以前相同,但结果会传递给上面验证DispoDts

的查询

就这样?

    DECLARE @DispoStartDt datetime, @DispoEndDt datetime, @DispoCode varchar(800)
    SET @DispoStartDt = '5-1-2015'
    --SET @DispoEndDt = '12-31-2014'
    SET @DispoCode = '_NULL'

    SELECT    c.CaseID
            , c.ProsNum AS FileNum
            , c.CaseStatusDesc AS CaseStatus
            , disp.DispoDts
    FROM jw50_Case c
        LEFT JOIN   (SELECT  c1.CaseID,
                              STUFF((SELECT '; ' + CONVERT(VARCHAR(10), ct.DispoDt, 101)
                                      FROM jw50_CountInvPers ct
                                      WHERE ct.CaseID = c1.CaseId
                                        AND     (ct.DispoDt >= DATEADD(dd, DATEDIFF(dd, 0, ISNULL(@DispoStartDt, ct.DispoDt)), 0) AND ct.DispoDt < DATEADD(dd, DATEDIFF(dd, -1, ISNULL(@DispoEndDt, ct.DispoDt)), 0))
                                      ORDER BY DispoDesc
                                      FOR XML PATH('')), 1, 1, '') [DispoDts]
                      FROM jw50_Case c1
                      WHERE c1.CaseID = c1.CaseId
                      GROUP BY c1.CaseID
         ) disp ON disp.CaseID = c.CaseID
    WHERE @DispoEndDt IS NULL OR disp.DispoDts IS NOT NULL
    ORDER BY CaseID, DispoDts