I have a bit of a challenge right now. I have the following table:
| ID | STATUS |
12 COMPLETE
12 FAIL
11 COMPLETE
What I need to select is the distinct ID where the status is complete. If the status is "Fail", I don't want to see that ID returned to me.
I have tried this:
select distinct id where status = 'COMPLETE'
This returns to me however all rows since the status for id 12 is complete on one row. I want it not to return that row since 12 also has a fail (I only want to return the row where all statuses for a given ID is "COMPLETE").
Does anybody have any pointers on this? Any help is greatly appreciated!
Best regards, Prince of Sweden
Just add a validation to make sure all rows have status "COMPLETE"
SELECT id
FROM status
GROUP BY id
HAVING count(id) = count( CASE WHEN status = 'COMPLETE' THEN 1 END )
i would try:
SELECT id
FROM status
WHERE status = 'COMPLETE'
GROUP BY id
This would work. The idea is to select the rows where status = complete as first phase. Then second phase is to validate if there is another status for the same ID in the table, where it is <> than complete. That means that it will only return the iteration where there is only one status of complete.
declare @table table (id integer, status varchar(max))
insert into @table select 12,'complete'
insert into @table select 12,'fail'
insert into @table select 11,'complete'
select ID, status
from @table t1
where not exists (select 1 from @table t2 where t2.id =t1.id and t1.status <> t2.status)
and t1.status = 'complete'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.