IchBinDicky
IchBinDicky

Reputation: 55

SQL Query to Return Unique Dates based on another Datetime column for the current date?

I have the following table (well the below is a scaled down example):

CID | LogDate       | RemDate  
1   | 2015-05-06    | 2015-10-01  
2   | 2015-05-06    | 2015-10-01  
2   | 2015-05-06    | 2015-10-30  
1   | 2015-01-03    | 2015-10-01  
1   | 2015-01-03    | 2015-10-01  
2   | 2015-04-06    | 2015-10-01  
2   | 2014-04-06    | 2015-10-01  
3   | 2015-05-06    | 2015-12-01  

I need to return only rows that have a unique [remdate] on the current date & ignore the others even when they have a matching [CID]....

I'd expect to have back the two unique remdates (2015-12-01 & 2015-10-30)

I hope I'm not missing something obvious here, any help or pointers would be greatly appreciated.

Upvotes: 3

Views: 47

Answers (2)

sgeddes
sgeddes

Reputation: 62861

If you only want to return those remdates that appear once in the table, you can use aggregation for that with count:

select remdate
from yourtable
group by remdate
having count(remdate) = 1

If you want to return those remdates that appear once per logdate, just add that field to your group by clause.

Upvotes: 4

Colin Schoen
Colin Schoen

Reputation: 2598

If you only need to remove duplicates then use DISTINCT.

SELECT distinct remdate, * FROM table

Upvotes: 0

Related Questions