gpt4 book ai didi

MySQL: order by inside group by

转载 作者:可可西里 更新时间:2023-11-01 07:16:48 25 4
gpt4 key购买 nike

我有一个像这样的 mysql 表:

mysql> select * from pt_onhand where pn = '000A569011';
+------------+-----+----+--------+------------+---------+--------------+-----+
| pn | pa | mn | ACTIVE | locate | onhand | avg_cost | whs |
+------------+-----+----+--------+------------+---------+--------------+-----+
| 000A569011 | P/A | | | AA-112 | 13.0000 | 0.0000000000| |
| 000A569011 | P/A | | | PF120136.1 | 1.0000 | 5.4785156200 | |
+------------+-----+----+--------+------------+---------+--------------+-----+

我想执行这样的查询:

mysql> select sum(onhand),max(locate),avg_cost from pt_onhand where pn = '000A569011' group by pn;
+-------------+-------------+--------------+
| sum(onhand) | max(locate) | avg_cost |
+-------------+-------------+--------------+
| 14.0000 | PF120136.1 | 0.0000000000|
+-------------+-------------+--------------+

所以我的问题是:我能否在同一个查询中获取与 max(locate) PF120136.1 相关的 avg_cost 5.4785156200,怎么做?谢谢

最佳答案

有点乱,但应该可以解决问题:

select a.onhand, a.locate, p.avg_cost
from
(select sum(onhand) onhand, max(locate) locate from pt_onhand where pn = '000A569011' group by pn) a
join pt_onhand p on p.locate = a.locate

关于MySQL: order by inside group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com