SQL Server – How to Check for Null End Date in Staff Records

countnullsql-server-2016

I have a database that records employees tasks throughout the organisation and when they finish etc., I need to identify if each employee, has at least one row with a null value in either finish.

Basically – show me any employee_no that does not have at least one row with a null value in column Finish.

There can be more than one null value but as long as there's one.

Sample database:

enter image description here

Best Answer

SELECT employee_no
FROM source_table
GROUP BY employee_no
HAVING COUNT(*) = COUNT(finish)

Is there a way to include the whole table in that sequence? – Chris

select serverproperty('ProductVersion') as [version], serverproperty('Edition') as [edition] and got Version 13.0.5102.14 of edition Devleoper Edition (64-bit) – Chris

This is (Microsoft) SQL server 2016 SP2. – Akina

WITH cte AS ( SELECT Employee_No,
                     Task,
                     Start,
                     Finish, 
                     MAX(CASE WHEN Finish IS NULL 
                              THEN 1 
                              ELSE 0 
                              END) OVER (PARTITION BY employee_no) have_nulls
              FROM source_table
            )
SELECT Employee_No,
       Task,
       Start,
       Finish
FROM cte
WHERE have_nulls = 0
/* ORDER BY 1,2,3 */
Related Question