-1

Query

SELECT userid, lastseen 
FROM user_login_management 
GROUP BY userid 
ORDER BY lastseen DESC

when I use group by with order by DESC then order by Desc not work.

userid is duplicated record and I'm trying to fetch the value of the latest row column named lastseen matching userid only.

If I use GROUP BY userid it well works and removes duplications but in this case, the lastseen column record is the first one of the matching id then I set ORDER BY lastseen DESC but it does not work.

If I use it without GROUP BY then it works. Is there any way to set GROUP BY on userid and ORDER BY DESC on*lastseen column?

GMB
  • 216,147
  • 25
  • 84
  • 135

1 Answers1

0

You need to use an aggregate for your lastseen column:

SELECT userid, max(lastseen) as lastseen 
    FROM user_login_management 
    GROUP BY userid 
    ORDER BY lastseen DESC
aynber
  • 22,380
  • 8
  • 50
  • 63