Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help

Author  Topic 

Angelorush
Starting Member

3 Posts

Posted - 2010-11-23 : 12:00:06

Employee Color

Employee1 Blue
Employee2 Red
Employee3 Blue
Employee3 Red

end result

Employee1 Blue Only
Employee2 Red Only
Employee3 Blue and Red

thanks in advance

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-23 : 12:11:57
What have you tried so far?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Angelorush
Starting Member

3 Posts

Posted - 2010-11-23 : 12:14:32
i've tried case statements but once i get a hit on the first statement

the results only shows this

Employee1 Blue Only
Employee2 Red Only
Employee3 Blue Only
Go to Top of Page

Angelorush
Starting Member

3 Posts

Posted - 2010-11-23 : 12:35:19
case
when color = 'Blue' then 'Blue Only'
when color = 'Red' then 'Red Only'
when color in ('Blue','Red') then 'Both'
end
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-23 : 12:50:56
you need a join

SELECT Employee
, COALESCE(Color1,'') + COALESCE(Color2,'')
FROM (SELECT DISTINCT Employee FROM Table WHERE Color = 'Blue') AS e
LEFT JOIN (SELECT Employee, Color1 FROM Table WHERE Color = 'Red') AS r
ON e. Employee = r.Employee
LEFT JOIN (SELECT Employee, Color2 FROM Table WHERE Color = 'Blue') AS b
ON e. Employee = b.Employee

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -