Skip to content
Advertisement

MySQL select unique records from one table with two select queries using NOT IN

TaskNumber JobNumber Status Date
11111 JS0001 0 04-06-2021
11112 JS0002 0 05-06-2021
11113 JS0003 1 05-06-2021
11114 JS0001 1 06-06-2021

Table Tasks. TaskNumber is the unique id(primary key). Status 0 is not done, 1 is done.

example: JobNumber JS0001 assigned on 04th of june and not done, so reassigned on 06th of june and was completed(status 1)

How do i get the unique job numbers that are not done using mysql query? (Only the Jobnumbers that are not completed for example JS0002 and not JS0001 on 04-06-2021)?

I have tried following statement but it just shows all records where status is 0, is it possible to write two select statements and compare using NOT IN?

SELECT DISTINCT Job_no FROM TASKS WHERE status=’0′

Thank you

Advertisement

Answer

I would use aggregation and having:

select jobnumber
from tasks
group by jobnumber
having max(status) = 0;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement