0

How can we get all the records apart from the least date (Min Date)

Assume this is my Input

ID DATE
1 23/03/1990
1 24/04/2016
1 25/05/2016
1 23/03/1990
1 25/05/2018

Expected Output

ID DATE
1 24/04/2016
1 25/05/2016
1 25/05/2018

Can we perform this operation in a single line query?

Annie Jeba
  • 373
  • 1
  • 3
  • 15

3 Answers3

1

Other ways outlined here

    SELECT ID, 
           DATE
    FROM (SELECT distinct 
                 ID,
                 DATE,
                 row_number() over (order by DATE) rnk
          FROM table_name)
    WHERE rnk >1;
Community
  • 1
  • 1
M O'Connell
  • 487
  • 5
  • 18
0

Join to a query that returns the min:

select t.id, date
from mytable t
left join (select id, min(date) min
           from mytable
           group by id) x
  on t.id = x.id and date = min
where x.id is null

It works by returning only missed joins.

This query will work on (virtually) any database.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Try this

select * from tableA where datecolmn not in (select min(datecolmn) as dt from tableA a group by ID)
Atul
  • 440
  • 8
  • 24