我们有一个数据集,其中包含一个名为"取消"这是一个布尔字段。我希望报告有一个名为@includecancelled的参数作为布尔值。用户可以将其标记为True或False,以控制是否在报告中包含已取消的记录。到目前为止,我还没有找到办法做到这一点。这是我目前所拥有的基本概要:
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
这显然不起作用,但总体上是我想要的。
答案 0 :(得分:2)
尝试使用CASE语句。 <强>已更新强>
*
答案 1 :(得分: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