如何编写Where子句以根据参数选择提供数据

时间:2014-07-21 14:36:43

标签: sql-server-2008

我正在使用SSRS和SSMS 2008 R2。我有2个报告,用户想要合并为1个报告,并提供选择选项 获取2013年或2014年数据的日​​期。两个报告中的所有数据集都是相同的,除了一个 每个报告中具有不同硬编码的数据集2013组的“子句”值与2014年客户组相对应。 我以为我可以在Where子句中执行CASE语句,根据@Startdate参数选择硬编码的2013组或2014组, 但这没有用,有关如何做到这一点的任何想法?

以下是2013年查询和2014年查询,其中Where子句是两份报告之间的唯一区别:

    SELECT [FISCAL YEAR]
,COUNT(*) AS EXPECTED
,SUM(Amount) AS Total
FROM dbo.Table1 T1
INNER JOIN dbo.Table2 T2
    ON T1.primarykey = T2.primarykey
WHERE Field1 IN (
        5555
        ,6666
        )
    AND Field2 IN (
        2222
        ,3333
        )
    AND YEAR(T1.DATE) = YEAR(@StartDate)
    AND T1.AMOUNT >= 25000
    AND T1.DESCRIPTION IN (
        'The Campaign - YYY - 2011-2015'
    ,'The Campaign - ZZZ - 2011-2015'
    ,'2013 AAA Goal'
    ,'2013 BBB Goal'
    ,'2013 CCC Goal'
    ,'2013 DDD Goal'
    ,'2013 EEE'
    ,'2013 FFF'
    )
GROUP BY RIGHT(CONVERT(VARCHAR, T1.DATE, 1), 2)

SELECT [FISCAL YEAR]
    ,COUNT(*) AS EXPECTED
    ,SUM(Amount) AS Total
FROM dbo.Table1 T1
INNER JOIN dbo.Table2 T2
    ON T1.primarykey = T2.primarykey
WHERE Field1 IN (
        5555
        ,6666
        )
    AND Field2 IN (
        2222
        ,3333
        )
    AND YEAR(T1.DATE) = YEAR(@StartDate)
    AND T1.AMOUNT >= 25000
    AND T1.DESCRIPTION IN (
        'The Campaign - YYY - 2011-2015'
        ,'The Campaign - ZZZ - 2011-2015'
        ,'2014 AAA Goal'
        ,'2014 BBB Goal'
        ,'2014 CCC Goal'
        ,'2014 DDD Goal'
        ,'2014 EEE'
        ,'2014 FFF'
        ,'2014 GGG'
        ,'2014 HHH'
        ,'2014 JJJ'
        ,'2014 LLL'
        )
GROUP BY RIGHT(CONVERT(VARCHAR, T1.DATE, 1), 2)

1 个答案:

答案 0 :(得分:0)

不知道你的表和数据库。我会在两个查询之间联合所有并在union上添加一个新年列的参数。

Select * from 
(
SELECT [FISCAL YEAR]
,COUNT(*) AS EXPECTED
,SUM(Amount) AS Total, YEAR(T1.DATE) as 'pYear'
FROM dbo.Table1 T1
INNER JOIN dbo.Table2 T2
    ON T1.primarykey = T2.primarykey
WHERE Field1 IN (
        5555
        ,6666
        )
    AND Field2 IN (
        2222
        ,3333
        )
    --AND YEAR(T1.DATE) = YEAR(@StartDate)
    AND T1.AMOUNT >= 25000
    AND T1.DESCRIPTION IN (
        'The Campaign - YYY - 2011-2015'
    ,'The Campaign - ZZZ - 2011-2015'
    ,'2013 AAA Goal'
    ,'2013 BBB Goal'
    ,'2013 CCC Goal'
    ,'2013 DDD Goal'
    ,'2013 EEE'
    ,'2013 FFF'
    )
GROUP BY RIGHT(CONVERT(VARCHAR, T1.DATE, 1), 2)
Union ALL
SELECT [FISCAL YEAR]
    ,COUNT(*) AS EXPECTED
    ,SUM(Amount) AS Total, YEAR(T1.DATE) as 'pYear'
FROM dbo.Table1 T1
INNER JOIN dbo.Table2 T2
    ON T1.primarykey = T2.primarykey
WHERE Field1 IN (
        5555
        ,6666
        )
    AND Field2 IN (
        2222
        ,3333
        )
    --AND YEAR(T1.DATE) = YEAR(@StartDate)
    AND T1.AMOUNT >= 25000
    AND T1.DESCRIPTION IN (
        'The Campaign - YYY - 2011-2015'
        ,'The Campaign - ZZZ - 2011-2015'
        ,'2014 AAA Goal'
        ,'2014 BBB Goal'
        ,'2014 CCC Goal'
        ,'2014 DDD Goal'
        ,'2014 EEE'
        ,'2014 FFF'
        ,'2014 GGG'
        ,'2014 HHH'
        ,'2014 JJJ'
        ,'2014 LLL'
        )
GROUP BY RIGHT(CONVERT(VARCHAR, T1.DATE, 1), 2)
) as t 
where pYear =YEAR(@StartDate)

希望这个帮助