Get last entry from each user in database

gattoo

I have a Postgresql database, and I'm having trouble getting my query right, even though this seems like a common problem.

My table looks like this:

CREATE TABLE orders (
   account_id   INTEGER,
   order_id     INTEGER,
   ts           TIMESTAMP DEFAULT NOW()
)

Everytime there is a new order, I use it to link the account_id and order_id.

Now my problem is that I want to get a list that has the last order (by looking at ts) for each account.

For example, if my data is:

account_id    order_id            ts
         5         178        July 1
         5         129        July 6
         4         190        July 1
         4         181        July 9
         3         348        July 1
         3         578        July 4
         3         198        July 1
         3         270       July 12

Then I'd like the query to return only the last row for each account:

account_id    order_id            ts
         5         129        July 6
         4         181        July 9
         3         270       July 12

I've tried GROUP BY account_id, and I can use that to get the MAX(ts) for each account, but then I have no way to get the associated order_id. I've also tried sub-queries, but I just can't seem to get it right.

Thanks!

Clodoaldo Neto
select distinct on (account_id) *
from orders
order by account_id, ts desc

https://www.postgresql.org/docs/current/static/sql-select.html#SQL-DISTINCT:

SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). Note that the "first row" of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Select Last Entry for each User, than the next entry

From Dev

Get last document from userchat for each user - mongoose

From Dev

Get last document from userchat for each user - mongoose

From Dev

Retrieve last entry for each

From Dev

How can I arrange this code so I can get different entry from database for each table row

From Dev

SQL get first and last transaction for each user

From Dev

Mongoose - find last message from each user

From Dev

Get First and Last Entry in Table by User within SQL Server

From Dev

Django Get Latest Entry from Database

From Dev

Get last message from each conversation

From Dev

SQL - Get last message from each conversation

From Dev

Get Last Message from Each Conversation sql

From Dev

How to get the last row from each users?

From Dev

Get Last Message from Each Conversation sql

From Dev

Get last record from each different id

From Dev

Get the last message from each conversation

From Dev

How to get the last value from the database

From Dev

How to get the last value from the database

From Dev

Efficient way to get last record from the database

From Dev

iOS Swift: retrieve an entry from the database for the user currently logged in

From Dev

get an entry from Infinispan cache without affecting last modify timestamp

From Dev

To get last month from user selected date

From Dev

How to get unique column value and get the first entry and last entry from a cursor

From Dev

Database design. And get last modified time with each column in PostgreSQL

From Dev

Codeigniter - Only allow the user to delete from database if the user created the entry in database

From Dev

Remove all entries except last 15 from database for each UserID

From Dev

Retriving 3 last records from database for each id

From Dev

Get a value for the last entry of a day

From Dev

how to get duplicate entry from database mysql with id duplicate

Related Related

  1. 1

    MySQL Select Last Entry for each User, than the next entry

  2. 2

    Get last document from userchat for each user - mongoose

  3. 3

    Get last document from userchat for each user - mongoose

  4. 4

    Retrieve last entry for each

  5. 5

    How can I arrange this code so I can get different entry from database for each table row

  6. 6

    SQL get first and last transaction for each user

  7. 7

    Mongoose - find last message from each user

  8. 8

    Get First and Last Entry in Table by User within SQL Server

  9. 9

    Django Get Latest Entry from Database

  10. 10

    Get last message from each conversation

  11. 11

    SQL - Get last message from each conversation

  12. 12

    Get Last Message from Each Conversation sql

  13. 13

    How to get the last row from each users?

  14. 14

    Get Last Message from Each Conversation sql

  15. 15

    Get last record from each different id

  16. 16

    Get the last message from each conversation

  17. 17

    How to get the last value from the database

  18. 18

    How to get the last value from the database

  19. 19

    Efficient way to get last record from the database

  20. 20

    iOS Swift: retrieve an entry from the database for the user currently logged in

  21. 21

    get an entry from Infinispan cache without affecting last modify timestamp

  22. 22

    To get last month from user selected date

  23. 23

    How to get unique column value and get the first entry and last entry from a cursor

  24. 24

    Database design. And get last modified time with each column in PostgreSQL

  25. 25

    Codeigniter - Only allow the user to delete from database if the user created the entry in database

  26. 26

    Remove all entries except last 15 from database for each UserID

  27. 27

    Retriving 3 last records from database for each id

  28. 28

    Get a value for the last entry of a day

  29. 29

    how to get duplicate entry from database mysql with id duplicate

HotTag

Archive