0

I'm trying to group the data to me that they just want the last record of each ID, what should I do?

In access to solve my problem this way:

SELECT Table1.id, Last(Table1.status) AS LastStatus, Last(Table1.date) AS LastDate
FROM Table1
GROUP BY Tabele1.id;

My table

 ____________________________
|  ID  | STATUS |    DATE    |
|123456|   1    | 15/04/2016 |
|123456|   2    | 16/04/2016 |
|123456|   3    | 17/04/2016 |
|123456|   1    | 18/04/2016 |
|654321|   3    | 19/04/2016 |
|654321|   4    | 20/04/2016 |
|654321|   2    | 21/04/2016 |
|98765 |   3    | 22/04/2016 |
|98765 |   1    | 23/04/2016 |
|98765 |   2    | 24/04/2016 |
|98765 |   3    | 25/04/2016 |
------------------------------

The desired result is the last record of each ID.

 ____________________________
|  ID  | STATUS |    DATE    |
|123456|   1    | 18/04/2016 |
|654321|   2    | 21/04/2016 |
|98765 |   3    | 25/04/2016 |
------------------------------
Rodrigo Prazim
  • 788
  • 7
  • 14

1 Answers1

0

You can use a where in with a subquery that return thx max date for the id

select * from my_table 
where (id, date)  in (  select id, max(date)  
                        from my_table 
                        group by id   )
order by date

or if you need order by status

select * from my_table 
where (id, date)  in (  select id, max(date)  
                        from my_table 
                        group by id   )
order by status
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107