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
 General SQL Server Forums
 New to SQL Server Programming
 CASE statement with AND?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 04/24/2013 :  06:03:25  Show Profile  Reply with Quote
I've got a query which ends with

AND ActivitySubTypeId IN (1, 6)


All works well except a new subtype has entered the mix and I need to vary this bit of code.

Basically, if ActivityTypeId = 3 then the bit of code above needs to be

AND ActivitySubTypeId IN (1, 6, 9)


otherwise it stays the same.....so it means having a dynamic AND (of sorts).

I've tied myself up in knots trying to combine a CASE with the AND.

Any advice appreciated, as always.

Edited by - Rasta Pickles on 04/24/2013 06:04:05

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 04/24/2013 :  06:12:35  Show Profile  Reply with Quote
Can you explain clearly? I'm not getting your point...

Follow these links to post your query

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
http://www.sqlservercentral.com/articles/Best+Practices/61537/

--
Chandu

Edited by - bandi on 04/24/2013 06:13:44
Go to Top of Page

Ifor
Aged Yak Warrior

590 Posts

Posted - 04/24/2013 :  06:17:21  Show Profile  Reply with Quote

AND
(
	ActivitySubTypeId IN (1, 6)
	OR
	(
		ActivityTypeId = 3 AND ActivitySubTypeId = 9
	)
)
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 04/24/2013 :  06:17:28  Show Profile  Reply with Quote

SELECT
TransactionDate,
ActivityTypeID,
ActivitySubtypeID,
SUM(ROUND(TransactionValue, 2))

FROM TRANSACTIONS

WHERE TransactionDate >= CONVERT(CHAR(8),DATEADD(MM,-1,GETDATE()),112)
AND TransactionDate <= CONVERT(CHAR(8),GETDATE(),112)

AND ActivitySubtypeId IN (1, 6)  

GROUP BY TransactionDate, ActivityTypeID, ActivitySubtypeID


If ActivityTypeID = 3 then the red bit of code needs to be

AND ActivitySubtypeId IN (1, 6, 9)
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 04/24/2013 :  06:28:47  Show Profile  Reply with Quote
I think Ifor's solution will work fine...

--
Chandu
Go to Top of Page

Ifor
Aged Yak Warrior

590 Posts

Posted - 04/24/2013 :  06:29:03  Show Profile  Reply with Quote
Do not do needless casts, they are expensive:

WHERE TransactionDate >= DATEADD(day, DATEDIFF(day, 0, DATEADD(month, -1, CURRENT_TIMESTAMP)), 0)
	AND TransactionDate <= DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP), 0)
	AND
	(
		ActivitySubTypeId IN (1, 6)
		OR
		(
			ActivityTypeId = 3 AND ActivitySubTypeId = 9
		)
	)
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 04/24/2013 :  06:41:52  Show Profile  Reply with Quote
Thanks for the input, I will try it later
Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
171 Posts

Posted - 04/26/2013 :  14:00:24  Show Profile  Reply with Quote
Just a follow-up, Ifor's code worked perfectly.

Many thanks, much appreciated.
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.11 seconds. Powered By: Snitz Forums 2000