lvella lvella - 6 months ago 10
SQL Question

How to get the order distinct rows were last inserted into a database?

This is a more to the point follow-up of my other question:



Given a table:

CREATE TABLE events (
order TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
value INT NOT NULL
);


At every insertion,
value
will be taken from a finite set.

How do I get the order all the elements of my set were last inserted into the database? I thought of doing
SELECT DISTINCT value FROM event ORDER BY "order" DESC;
, but according to answer in my other question, this won't work.

Answer

Answer from your other post.

Using max and grouping on the user you get the most recent timestamp by user.

SELECT 
    MAX([order]) AS MaxOrd
  , value
FROM Event
GROUP BY value
ORDER BY MaxOrd DESC