I have a table related to reviews made by a person. The table has the following fields: reviewId , personId , isComplete , where isComplete is a boolean indicating whether the particular person completed his review.
Imagine the following values:
ReviewID | PersonID | isComplete |
1 1 1
2 1 1
3 2 0
4 2 0
5 3 1
6 3 0
In this case I should get only PersonID = 1 as a result because only they have completed all their reviews. I have tried many queries and the closest one was: SELECT * FROM reviews x WHERE 1 = ALL (SELECT isComplete FROM reviews y WHERE x.personid = y.personid AND isComplete=1);
Any suggestions or hints will be greatly appreciated.
.
SELECT Distinct A.PersonID
FROM TABLE A
LEFT JOIN Table B
on A.PersonID = B.PersonId
and B.isComplete = 0
WHERE B.PersonId is null
I used distinct to only return 1 records.
Another way to do this (I believe to be the most efficient) would be to use an exists statement
SELECT Distinct A.PersonID
FROM table A
WHERE not exists (Select 1 from Table B where B.iscomplete=0 and A.PersonID=B.PersonID)
This basically says return all persons who don't have an incomplete review.
The premise in both these cases is that a single entry of an incomplete review is enough to exclude them from the result set.
SELECT DISTINCT(PersonID) FROM reviews
WHERE PersonId NOT IN (
SELECT DISTINCT(PersonID) FROM reviews WHERE isComplete = 0
)
There is more than one way to do this.
SELECT * FROM reviews a WHERE a.PersonId NOT IN
( SELECT b.PersonId FROM reviews b WHERE b.isComplete = 0 )
This is getting all the persons that match isComplete = 0 and then only including the persons that aren't in that list.
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.