首页 > 解决方案 > 如何从两个不同表的两个不同列中获取 MAX

问题描述

我有表 A、表 B、表 C,并且想从 B 和 MAX(date) 中获取 MAX(OrderId) 和 group by,并从 C 中获取 group by

表 A:

SNO
AccNo

表 B:

SNO(FK)
OrderId

表 C:

AccNo(FK)
date

我写了这样的东西:

select b.max(OrderId), other columns
from a, b, c
where a.sno = b.sno
and a.accno = c.accno
and (b.orderid, c.date) IN (select b.orderid, max(c.date)
from a, b,c 
where a.sno = b.sno
and a.accno = c.accno
group by b.orderid)
group by other columns

不知何故,这运行良好,但没有给出预期的结果。将不胜感激一些帮助。

标签: sqloracleoracle11goracle10goracle-sqldeveloper

解决方案


You can do:

select a.AccNo, max(b.orderId) as maxOrderId, max(myDate) as maxDate
from tableA a
inner join tableB b on a.SNO = b.SNO
inner join tableC c on a.AccNo = c.AccNo
group by a.AccNo

Is this what you wanted?


推荐阅读