2016-11-22 60 views
0

我們有一個數據集,其中包含一個名爲「取消」的字段,它是一個布爾字段。我希望報告具有一個名爲@includecancelled作爲布爾值的參數。用戶可以將其標記爲True或False來控制是否在報告中包含取消的記錄。到目前爲止,我還沒有找到辦法做到這一點。這是我目前擁有的基本輪廓:SSRS - 包含/排除某些記錄的參數

SELECT TOP 10 COUNT (table1.field1) as Count_field1 
    ,table2.fielda 
    ,table2.fieldb 
FROM 
    table1 
    INNER JOIN table2 
    ON table1.field1 = table2.fieldc 
WHERE 
    table1.field2 in (@param1) 
GROUP BY 
    table2.fielda 
    ,table2.fieldb 
ORDER BY COUNT (table1.field1) DESC 

我想有WHERE子句中的另一語句來排除或包括基於「取消」字段和用戶選擇的參數記錄。所以,這樣的事情:

SELECT TOP 10 COUNT (table1.field1) as Count_field1 
    ,table2.fielda 
    ,table2.fieldb 
FROM 
    table1 
    INNER JOIN table2 
    ON table1.field1 = table2.fieldc 
WHERE 
    table1.field2 in (@param1) 
    IF (@includecancelled = TRUE, '', ',AND table1.cancelled = FALSE') 
GROUP BY 
    table2.fielda 
    ,table2.fieldb 
ORDER BY COUNT (table1.field1) DESC 

這顯然不工作,但是我想要的一般想法。

回答

2

嘗試使用CASE語句。 修訂

SELECT TOP 10 COUNT (table1.field1) as Count_field1 
    ,table2.fielda 
    ,table2.fieldb 
FROM 
    table1 
    INNER JOIN table2 
    ON table1.field1 = table2.fieldc 
WHERE 
    table1.field2 in (@param1) 
    AND table1.cancelled = CASE WHEN @includecancelled = 1 THEN table1.cancelled ELSE 'FALSE' END 
GROUP BY 
    table2.fielda 
    ,table2.fieldb 
ORDER BY COUNT (table1.field1) DESC 
+0

(upvoted)正確的目標。 –

+0

這幾乎就在那裏,但有一個問題。當他們將「includecancelled」標記爲true時,我想返回所有值(而不僅僅是取消的值)。你的CASE的FALSE部分是現貨。但TRUE部分只返回取消的值。 – MSCF

+0

已更新上面。將table1.cancelled設置爲自己,而不是「TRUE」。 –

1
SELECT TOP 10 COUNT (table1.field1) as Count_field1 
    ,table2.fielda 
    ,table2.fieldb 
FROM table1 
    INNER JOIN table2 
    ON table1.field1 = table2.fieldc 
WHERE 
    table1.field2 in (@param1) 
    and (( @includecancelled = TRUE ) 
    OR (@includecancelled = FALSE AND table1.cancelled = FALSE)) 
GROUP BY table2.fielda,table2.fieldb 
ORDER BY COUNT (table1.field1) DESC 
相關問題