asinha
asinha

Reputation: 337

Select distinct value from a table

In my SQL Server database, I have a table with this sample data:

ID  lastDate
---------------
1   null
1   null
1   null
2   null
2   2018-12-14
2   null
3   null
4   null
4   2018-12-14
4   2018-12-14

I need to retrieve distinct ID whose lastDate value is null (i.e 1 & 3 only )

Please let me know the SQL query for this

Upvotes: 0

Views: 46

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270993

You can use aggregation and having:

select id
from t
group by id
having max(lastdate) is null;

Or:

having count(lastdate) = 0

Upvotes: 3

Related Questions