Solved

CASE statement SQL (using IN )

Posted on 2011-02-15
2
353 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

23 Experts available now in Live!

Get 1:1 Help Now