简体   繁体   English

获取Postgresql中相关表数据的最大值

[英]Getting max value of related table data in Postgresql

i am trying to get next:我正在尝试下一个:

I have a table orders.我有一张桌子订单。 This table has column order_uuid.此表有列 order_uuid。 I have another table status_history wich has all the statuses of each order.我有另一个表 status_history ,其中包含每个订单的所有状态。

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 order1 的状态为 1,2,3 order2 的状态为 4,5,6

it must return order1, 3 and order2 and 6.它必须返回 order1、3 和 order2 和 6。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM