Very simple.
msg_id msisdn message
----------------------------------
1000 661000 text1
1001 661002 text2
1002 661004 text3
1003 661002 text4
1004 661002 text5
1005 661002 text6
I need to get distinct msisdn values ordered from newest to oldest and all values in that last row. Please understand that there are more coluns in the real query so I need to get the distinct msisdn's - but also ALL DATA from last row with that msisdn.
msisdn msg_id message
-----------------------------
661002 1005 text6
661004 1002 text3
661000 1000 text1
Thanks!
Just to be clear. I started with:
select distinct(msisdn), max(msg_id) from table group by msisdn
However pulling all info about max(msg_id) is the problem... I need that whole row in the query, not just msg_id...
Hope it's all clearer now.
You can use a derived table containing the max msg_id
per msisdn
, then join to this table to get the rest of the columns:
select t1.*
from mytable as t1
inner join (
select msisdn, max(msg_id) as last_id
from mytable
group by msisdn
) as t2 on t1.msisdn = t2.msisdn and t1.msg_id = t2.last_id
select msisdn,msg_id,max(message)
from table1
group by msg_id,msisdn
order by msg_id desc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.