Reputation: 1193
If I have a table with three columns
userid,eventid,entrytime
and values like
userid,eventid,entrytime
1 1 NULL
2 1 2012-01-01
1 2 2012-01-01
2 2 2012-01-01
3 1 NULL
3 2 NULL
What is the best way of selecting userids where entrytime has always been NULL
Upvotes: 1
Views: 111
Reputation: 2714
SELECT DISTINCT t1.UserId
FROM <TableName> t1
LEFT JOIN <TableName> t2 ON t1.UserID = t2.UserID AND t2.EntryTime IS NOT NULL
WHERE t2.UserID IS NULL
Upvotes: 1
Reputation: 24046
try this:
select userid from
(select userid,entrytime,count(*) from yourtable
group by userid,entrytime)a
group by userid
having count(*)=1 and max(entrytime) is null
Upvotes: 3
Reputation: 270617
Since a COUNT(entrytime)
aggregate will eliminate NULL values, you can use it to determine which userid
has no non-null value for entrytime
by comparing it to 0 in the HAVING
clause.
SELECT userid, COUNT(entrytime)
FROM yourtable
GROUP BY userid
HAVING COUNT(entrytime) = 0
Here is a live demonstration (Result is userid
= 3)
Upvotes: 4