Home > Blockchain >  How to get the last date of every rows when grouped in Sqlite?
How to get the last date of every rows when grouped in Sqlite?

Time:05-10

If I make a GROUP BY auth_user.id it gives me the list. If the users who are grouped has more than one rows I get the date when the user filled my form first time. If I order it by date (DESC), it orders it by the date when of the first time he filled the form.

How can I get the date of the last time when he filled the form and the users are grouped? Now I'm here:

SELECT * FROM answers
ORDER BY date DESC

It gives all the entries of all users and every dates descending. It's okay.

SELECT * FROM answers
GROUP BY user_id
ORDER BY date DESC

Now it gives grouped data but with the first date of every users. How can I get the last date if grouped?

CodePudding user response:

You can use max(date) to get the most recent date for each user.

SELECT
  user_id,
  MAX(date)
FROM answers
GROUP BY user_id,
ORDER BY date DESC;
  • Related