2014-07-21 74 views
0

我正在使用SSRS和SSMS 2008 R2。我有2個報告,用戶想要合併爲1個報告,並選擇 日期以獲取2013或2014年的數據。兩個報告中的所有數據集都是相同的,每個報告中有一個 數據集具有不同硬編碼Where Where子句2013組與2014年客戶羣的價值。 我以爲我可以在Where子句中選擇CASE語句來選擇基於@Startdate參數的硬編碼2013組或2014組,但這不起作用,關於如何執行此操作的任何想法?如何編碼Where子句以根據參數選擇傳遞數據

這裏是2013查詢和2014年查詢的WHERE子句的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' 
    ,'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) 
+0

任何想法鄉親? – DataDude

回答

0

不知道你的表和數據庫。我會結合兩個查詢之間的所有參數,並在新的年份列上添加參數。

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) 

希望這有助於

+0

謝謝你的幫助! P.DATE_EXPECTED在選擇列表中無效,因爲它不包含在Group BY列表中。 (對於T1.Date,我使用'FY'+ RIGHT(CONVERT(VARCHAR,P.DATE_EXPECTED,1),2)+' - YTD'AS [FISCAL YEAR]。所以我將P.DATE_EXPECTED添加到Group By。然後我得到了16行,重複數據爲2組8個行,總計不同,每8行計數一次。我需要一行返回總計計數,並累計每個會計年度的金額。像會計年度= 2014財年預期= 24總計= 24,000,000美元pYear = 2014 – DataDude

+0

請修改我的回答,以幫助其他人。謝謝 – Hiten004