Help needed with SQL Syntax

Hi
Using SQL 2005/2008
I had a semi workable solution previously using RANK OVER PARTITION BY but can't get it working.

example is:

ID|Category
1|a
1|b
1|b
2|a
2|b
2|c
2|d
2|e
3|a
3|b
3|c
3|d

I need to flag the records where all records meet the condition that they have all categories a,b,c,d,e,
so result would be:

ID|CAT|FLAG
1|a|no
1|b|no
1|b|no
2|a|yes
2|b|yes
2|c|yes
2|d|yes
2|e|yes
3|a|no
3|b|no
3|c|no
3|d|no


Thanks
Jimmy_incAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasianCommented:
DECLARE @t TABLE (ID int, Category varchar(100))

INSERT @t 
SELECT 1,'a'
UNION ALL SELECT 1,'b'
UNION ALL SELECT 1,'b'
UNION ALL SELECT 2,'a'
UNION ALL SELECT 2,'b'
UNION ALL SELECT 2,'c'
UNION ALL SELECT 2,'d'
UNION ALL SELECT 2,'e'
UNION ALL SELECT 3,'a'
UNION ALL SELECT 3,'b'
UNION ALL SELECT 3,'c'
UNION ALL SELECT 3,'d'

SELECT *
	,flag=CASE WHEN
		5=(SELECT COUNT(DISTINCT Category) FROM @t WHERE ID=T.ID AND Category IN ('a','b','c','d','e'))
		THEN 'yes'
		ELSE 'no'
	END
FROM @t T

Open in new window

0
Jimmy_incAuthor Commented:
Thanks mate I will look into it
0
LowfatspreadCommented:
like this


select a.id,a.category as cat,coalesce(b.flag,'No') as Flag
from yourtable as a
left outer join (select id,'YES' as Flag from yourtable group by id having count(distinct category)=5)  as b
on a.id=b.id
order by 1,2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.