Skip to content
Advertisement

SQL Query to return all rows with the earliest date available for each year

I am trying to design a SQL query that can return the records for the earliest date in each year. Suppose I have a table as follows:

Date       | Category | Value
========== | ======== | =====
2019-01-03 | Apple    | 5
2019-01-03 | Orange   | 2
2019-01-20 | Apple    | 5
2019-01-20 | Orange   | 8
2019-02-05 | Apple    | 1
2019-02-05 | Peach    | 5
2018-01-02 | Apple    | 2
2018-01-02 | Orange   | 9
2018-05-10 | Apple    | 3
2018-05-10 | Orange   | 5
2018-07-20 | Apple    | 6
2018-07-20 | Orange   | 1

I am trying to generate a table shown below:

Date       | Category | Value
========== | ======== | =====
2019-01-03 | Apple    | 5
2019-01-03 | Orange   | 2
2018-01-02 | Apple    | 2
2018-01-02 | Orange   | 9

The earliest date for each year will change, which means I cannot simply query by day & month. I have tried using:

SELECT MIN(Date), * 
FROM mytable 
GROUPBY YEAR(Date)

But this results in an aggregate error: ‘Category’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

What would be the best way to achieve this?

Advertisement

Answer

A simple way is a correlated subquery:

select t.*
from mytable t
where t.date = (select min(t2.date)
                from mytable t2
                where t2.category = t.category and year(t2.date) = year(t.date)
               );

You can also use row_number():

select t.*
from (select t.*,
             row_number() over (partition by category, year(date) order by date) as seqnum
      from mytable t
     ) t
where seqnum = 1
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement