SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 case inside having
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1416 Posts

Posted - 12/19/2012 :  12:32:37  Show Profile  Reply with Quote
Hi,
Is it possible to place case inside having clause?
for example:
select field1, field2, sum(field3)
from tblMain
group by field1, field2
having sum(field3) > 0.1

I would like to do something like:
select field1, field2, sum(field3)
from tblMain
group by field1, field2
case when @param=1 then having sum(field3) > 0.1

so only do the having bit when @param = 1
Any thoughts please?
Thanks

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/19/2012 :  12:51:33  Show Profile  Reply with Quote
Would this work for you?
SELECT field1,
       field2,
       SUM(field3)
FROM   tblMain
GROUP BY
       field1,
       field2
HAVING
	(SUM(field3) > 0.1 AND @param = 1)
	OR 
	ISNULL(@param,0) <> 1;
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
372 Posts

Posted - 12/21/2012 :  17:29:38  Show Profile  Reply with Quote
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.
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1416 Posts

Posted - 12/22/2012 :  04:13:47  Show Profile  Reply with Quote
Thank you
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000