表的最後日期:SQL:加入表
MSUB - > ID,收到
msublist - > ID,sub_id,ITEM_ID,數量
我敢嘗試。
select a.sub_id
, a.item_id
, a.qty
, b.id
, b.receive_date
from msublist a
join (select x.id
, x.receive_date
from msub x
where x.receive_date = (select max(x1.receive_date)
from msub x1
where x1.id = x.id)) b
on (a.sub_id = b.id)
order by a.item_id,b.receive_date desc
it'not幹活想成爲ITEM_ID
看起來像x.id沒有在你的第二個子查詢中定義最大日期。你得到了什麼確切的錯誤? – gaganshera
您正在使用哪些DBMS? Postgres的?甲骨文? –
http://stackoverflow.com/questions/tagged/greatest-n-per-group+sql –