0

I have a table in the database that i'd like to retrieve distinct values in an order from the oldest to the most recent.

The closest result I got was this using this sql command:

SELECT userID FROM commande ORDER BY date_commande ASC, userID ASC;
userID
1
1
2
2
3
3
2
1
1
2
2

As you can see, the values are repetitive, i want to get a result that doesn't repeat when the items I retrieve have the same userID and datetime:

userID
1
2
3
2
1
2

I appreciate your help,

Full table: Name of the table: Commande

ÜNKNØWÑ
  • 11
  • 3
  • Does this answer your question? [SQL to find the number of distinct values in a column](https://stackoverflow.com/questions/141562/sql-to-find-the-number-of-distinct-values-in-a-column) – mmh4all Apr 12 '23 at 07:41
  • If a userID has several dates, which of them should be ordered by? – jarlh Apr 12 '23 at 07:41
  • @jarlh basically, if the userID have the same dates, then it only shows one of them and then orders from oldest to most recent – ÜNKNØWÑ Apr 12 '23 at 07:55
  • @mmh4all Thanks for your reply, Unfortunately, it doesn't answer my question – ÜNKNØWÑ Apr 12 '23 at 08:17
  • Provide source data which produces shown output as INSERT INTO code. – Akina Apr 12 '23 at 08:41

1 Answers1

1
SELECT userID 
FROM (
    SELECT DISTINCT userID, date_commande
    FROM commande 
    ) subquery
ORDER BY date_commande ASC, userID ASC;
Akina
  • 39,301
  • 5
  • 14
  • 25