如何从 Oracle SQL 中的 2 个表中提取每个 ID 的最新记录
how to pull latest record for each ID from 2 tables in Oracle SQL
我有 2 个 table 命名为 - Product 和 sold_product
sold_product
product_code sold_date product_id product_rating
46077862546 18/08/21 380 3.5
41237862546 18/08/21 300 5.0
41237862789 06/08/21 356 4.0
产品
product_id product_name rack_no
380 Television 5
344 Refridgerator 4
333 Air Conditioner 6
现在我需要为每个 product_id 和 product_code 和 product_name 提取最新的 sold_date。
每个 product_id 在产品 table.
中都有一个(唯一的)条目
试试这个:
select ap.prid,ap.prcode, a.name, ap.MaxDate from product a,(select prCode,prid, max(soldDate) as MaxDate
from sold_product
group by prid) ap where a.id=ap.prid;
像下面这样尝试:
select p.product_id, s.product_code, p.product_name, max(s.sold_date)
from product p
left join sold_product s on s.product_id = p.product_id
group by p.product_id, s.product_code, p.product_name
我有 2 个 table 命名为 - Product 和 sold_product
sold_product
product_code sold_date product_id product_rating
46077862546 18/08/21 380 3.5
41237862546 18/08/21 300 5.0
41237862789 06/08/21 356 4.0
产品
product_id product_name rack_no
380 Television 5
344 Refridgerator 4
333 Air Conditioner 6
现在我需要为每个 product_id 和 product_code 和 product_name 提取最新的 sold_date。 每个 product_id 在产品 table.
中都有一个(唯一的)条目试试这个:
select ap.prid,ap.prcode, a.name, ap.MaxDate from product a,(select prCode,prid, max(soldDate) as MaxDate
from sold_product
group by prid) ap where a.id=ap.prid;
像下面这样尝试:
select p.product_id, s.product_code, p.product_name, max(s.sold_date)
from product p
left join sold_product s on s.product_id = p.product_id
group by p.product_id, s.product_code, p.product_name