Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,Is it possible to place case inside having clause?for example:select field1, field2, sum(field3)from tblMaingroup by field1, field2having sum(field3) > 0.1I would like to do something like:select field1, field2, sum(field3)from tblMaingroup by field1, field2case when @param=1 then having sum(field3) > 0.1so only do the having bit when @param = 1Any thoughts please?Thanks
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-12-19 : 12:51:33
Would this work for you?
SELECT field1, field2, SUM(field3)FROM tblMainGROUP BY field1, field2HAVING (SUM(field3) > 0.1 AND @param = 1) OR ISNULL(@param,0) <> 1;
ScottPletcher
Aged Yak Warrior
550 Posts
Posted - 2012-12-21 : 17:29:38
Yes, you can use CASE in a HAVING clause.But keep in mind that a CASE statement must evaluate down to a single value, and cannot include any operators or keywords.