user3200361 user3200361 - 5 days ago 6
SQL Question

Get rows which have a specific value occurring less than twice

I have an issue with a SQL query. I have a table as below

EMPLOYEE_ID | STATUS
--------------------
123 C
234 S
456 S
123 C
789 S


From the above table I need to get
EMPLOYEE_ID
which have occurrences with Status
C
appearing less than twice. Need help in figuring this out.

Answer

I'm not Oracle, but see if you can use this.

Select Employee_ID
From table
Where Status = 'C'
Group By Employee_ID
Having Count(Status) < 2
Comments