Getting max value of related table data in PostgreSQL

Viewed 24

I am trying to get next:

I have a table orders. This table has column order_uuid. I have another table status_history which has all the statuses of each order.

I need to get all the order and the max value of all the statuses of each order.

Example:

order1 has statuses 1,2,3
order2 has statuses 4,5,6

it must return order1, 3 and order2 and 6.

1 Answers

You didn't give us all the needed column names but try something like this:

SELECT o.ordernumber, max(sh.status)
FROM orders o
JOIN status_history sh ON sh.order_uuid = o.order_uuid
GROUP BY o.ordernumber
Related