Reputation: 153
I have set of data in Trans
table. Which contains several transactions of transno
. I need to get latest transaction record of each transno
.
These data stored in Oracle database.
I have tried below query, with few changes in every time. But i gives only one raw. This table contain more than 1m records.
select * from (select transid,transno,transdate,transtype
from trans order by TRANSID desc) where rownum <= 1
Please help on this.
Upvotes: 4
Views: 696
Reputation: 10617
Let me know if this works.
SELECT * FROM trans GROUP BY transno ORDER BY transid DESC LIMIT 1
I'm not a MySQL master, so let me know.
Upvotes: 0
Reputation: 2085
The best way for this solution is @Prdp's way. But there is another way too. You can use inline view
like this:
select * from
trans t
inner join
(
select transno, max(transdatetime) maxtransdatetime from trans group by transno
) s
on s.transno = t.transno and s.maxtransdatetime = t.transdatetime
Upvotes: 0
Reputation: 93754
You need to use ROW_NUMBER
window function to get the latest transdate
for all the transno
select * from
(
select transid,transno,transdate,transtype,
Row_number()over(partition by transno order by transdate desc) as rn
from trans
) where RN = 1
Upvotes: 3