Search code examples
oracle-databasegroup-bydistinctusing

Distinct Dates Group BY Year


How to find the distinct dates in a year using Oracle?

id    sent_date
1     2017-05-01 
1     2017-05-01 
1     2017-06-01
1     2016-06-01

Ignore the duplicate sent_date for a Id in the same year.

Output

count(*)   id   year
2           1   2017
1           1   2016

EDIT:

Thisis my query

select distinct(count(sent_date)), id , extract (year from sent_date)
from test
GROUP BY id, extract (year from sent_date). 

3 1 2017 (wrong)-- expecting the count as 2 1 1 2016 –


Solution

  • The DISTINCT is wrongly positioned in your query; you simply need:

    select count(distinct sent_date), id , extract (year from sent_date)
    from test
    group by id, extract (year from sent_date)
    

    Also, DISTINCT is not a function, so the syntax DISTINCT(...) does not make sense.