I have a query that return employees:
Name Form
Bob abc
Bob gfd
Bob fgf
John abc
Gavin abc
Jessie ala
Jessie asf
How would I say if abc exists for the employee then result = yes and if abc doesnt exist then no? What I would want to see is this:
Name 开发者_如何学JAVA Result
Bob Yes
John Yes
Gavin Yes
Jessie No
SQL Server 2000+, use:
SELECT e.name,
COALESCE(MAX(CASE WHEN e.form = 'abc' THEN 'Yes' END), 'No') AS result
FROM EMPLOYEES e
GROUP BY e.name
The MAX portion will return NULL if none of the form values match "abc", which the COALESCE catches to return "No".
精彩评论