开发者

SQL group where clause by employee

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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜