Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL group and order

Tags:

sql

grouping

I have multiple users with multiple entries recording times they arrive at destinations

Somehow, with my select query I would like to only show the most recent entries for each unique user name.

Here is the code that doesn't work:

SELECT * FROM $dbTable GROUP BY xNAME ORDER BY xDATETIME DESC

This does the name grouping fine, but as far as showing ONLY their most recent entry, is just shows the first entry it sees in the SQL table.

I guess my question is, is this possible?

Here is my data sample:

john  7:00
chris 7:30
greg 8:00
john 8:15
greg 8:30
chris 9:00

and my desired result should only be

john 8:15
chris 9:00
greg 8:30
like image 474
John Lambert Avatar asked Apr 24 '26 11:04

John Lambert


2 Answers

How about something like

Select xName, MAX(xDATETIME) AS MaxDateVal
FROM $dbtable
GROUP BY xName
ORDER BY MaxDateVal
like image 119
Adriaan Stander Avatar answered Apr 26 '26 00:04

Adriaan Stander


SELECT xNAME, MAX(xDATETIME)
FROM $dbTable 
GROUP BY xNAME 
ORDER BY xDATETIME DESC
like image 28
Dustin Laine Avatar answered Apr 26 '26 02:04

Dustin Laine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!