Solved

CASE statement SQL (using IN )

Posted on 2011-02-15
2
352 Views
Last Modified: 2012-05-11
I am trying to identify an individual based on codes in our database. I need to assign a name to a person based on what the individuals codes are for that year. Donations.

I am trying to use an in statement in a case statement.. I can't seem to find any information on this being possible or not out there. It is not looking good though :(.

Escencially, what I need to find out is by grouping by donor and looking at all of the donors records. if they have given specific procedures..

Please see the code and I will be happy to explain in more detail what I am trying to do if you are confused..
select donor_id, donation_procedure,
CASE donation_procedure
WHEN donation_procedure IN ('PL', 'PE', 'PR', 'PP', 'RP','DR', 'LV', 'WB', 'HH', 'AU', 'CU', 'SM', 'Q0', 'TH' THEN 'REDCELL' 
WHEN (donation_procedure IN ('PL', 'PE', 'PR', 'PP', 'PR')) AND (donation_procedure) NOT IN ('DR', 'LV', 'WB', 'HH', 'AU', 'CU', 'SM', 'Q0', 'TH')) THEN 'APHERESIS'
ELSE 'UNKNOWN' END
from TABLE_INFO
where  (collection_date >= DATEADD(day, - 365, GETDATE()))
group by donor_id, donation_procedure
order by donor_id

Open in new window

0
Comment
Question by:Jennifer Barman
2 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34900594
try like this
select donor_id, donation_procedure,
	CASE 
	WHEN ((donation_procedure IN ('PL', 'PE', 'PR', 'PP')) AND 
	      (donation_procedure) NOT IN ('DR', 'LV', 'WB', 'HH', 'AU', 'CU', 'SM', 'Q0', 'TH')) THEN 
		'APHERESIS'
	WHEN donation_procedure IN ('PL', 'PE', 'PR', 'PP', 'RP','DR', 'LV', 'WB', 'HH', 'AU', 'CU', 'SM', 'Q0', 'TH') THEN 
		'REDCELL' 	
	ELSE 
		'UNKNOWN' 
	END
from TABLE_INFO
where  (collection_date >= DATEADD(day, - 365, GETDATE()))
group by donor_id, donation_procedure
order by donor_id

Open in new window

0
 

Author Closing Comment

by:Jennifer Barman
ID: 34900614
THANKS!! :) worked perfectly! :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This is about my first experience with programming Arduino.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now