开发者

SQL group where clause by employee

开发者 https://www.devze.com 2023-01-25 09:13 出处:网络
I have a query that return employees: NameForm Bobabc Bobgfd Bobfgf Johnabc Gavinabc Jessieala Jessieasf How would I say if abc exists for the employee then result = yes and if abc doesnt exist the

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

精彩评论

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