SQL Count function returning inaccurate results

countoracleselect

I am trying to write a query in oracle to list the number of records contained in a single table filtered by multiple columns in the tablespace. Here is the query syntax I have come up with:

SELECT COUNT(CASE WHEN unique_id like '%OU=Users%' and employee_type > '-1' THEN 1 ELSE NULL
         END) AS USERS1
   ,COUNT(CASE WHEN unique_id like '%OU=Vendors%' and employee_id is null and employee_type is null THEN 1 ELSE NULL
          END) AS USERS2
   ,COUNT(CASE WHEN unique_id like '%OU=Temp Users%' and employee_type ='-1' THEN 1 ELSE NULL
          END) AS USERS3
   ,COUNT(CASE WHEN unique_id like '%OU=Service%' THEN 1 ELSE NULL
          END) AS USERS4
   ,COUNT(*) AS USERS5
FROM table_USERS 
WHERE is_terminated = 'False'

Here is the resulting output:

users1=1192
users2=38
users3=25
users4=240
users5=1548

The result is not correct it is showing "users5" as the total number of users that exist in the database (1548), when it should show me only 53 users which don't match any of the user categories (users1 – users4).

What am I missing and is there a more simplistic way to write this query?

Best Answer

It looks like you just have "COUNT(*) AS USERS5", I'm not seeing where you're filtering out the other types of user categories.