I am using Access 2013. Table1 has PK quote_id and is a list of quotes for different parts sourced from different vendors. How do I pull the top 2 most recent quotes for every part,vendor combination?
This is what I am currently using, but in terms of performance it is very inefficient. Is there a better query to achieve this? Thanks
SELECT a.part_id, a.vendor_id, a.quote_date
FROM Table1 AS a
WHERE a.quote_date > DATEADD("yyyy", -3, DATE()) AND
a.quote_date IN
(SELECT TOP 2 quote_date
FROM Table1
WHERE quote_date > DATEADD("yyyy", -3, DATE()) AND
part_id=a.part_id AND vendor_id=a.vendor_id
ORDER BY quote_date DESC)
答案 0 :(得分:0)
This should work but no way to test it from here -
Select part_id, vendor_id, quote_date From (SELECT a.part_id, a.vendor_id, a.quote_date, Row_Number() Over (Partition By a.part_id, a.vendor_id Order By a.quote_date desc) as quote_Date_Seq FROM Table1 AS a WHERE a.quote_date > DATEADD("yyyy", -3, DATE()) b Where b.quote_Date_Seq <= 2