SQLTeam.com | Weblogs | Forums

Selecting row with the most recent timestamp

Hello All!
I have a database for a shop with date+time stamps of when items were purchased, I'd like to find the value of the last item that was purchased across multiple products.

How do I go about this?

Tried below but it's not working for me

Select product_id, price
FROM Fruitshop
WHERE
order_at = (SELECT max(order_at) FROM Fruitshop)

I also tried

Select
product_id,
price,
max (order_at)
from Fruitshop

This will give you the last row from the Fruitshop table:

SELECT fs.*
FROM (
    Select *, ROW_NUMBER() OVER(ORDER BY order_at DESC) AS row_num
    FROM Fruitshop
) AS fs
WHERE row_num = 1

Thank you @ScottPletcher

Pls what the fs.* nomenclature means?

Also the query is returning an error not sure why... I use MySQL

The ROW_NUMBER function does not work for me

this is a Microsoft SQL Server forum @Mary