在价目表中查找最新价格



我有一个包含项目、价格和价格日期列的表格。 我需要做的是只过滤掉旧价格,只显示每个项目的最新价格。我正在使用MS Access 2007,需要在我正在编写的某些VBA中引用最新的价格,并且在我的研究中,我找不到以编程方式访问数据透视表数据的方法,以便在数据库的其他地方进行客户定价。

例:

桌子

项目 |价格 |价格日期

您可以使用相关的子查询:

select t.*
from t
where t.pricedate = (select max(t2.pricedate)
from t as t2
where t2.item = t.item
);

一个选项使用子查询来查找每个项目的最新价格:

SELECT t1.Item, t1.Price, t1.PriceDate
FROM yourTable t1
WHERE t1.PriceDate = (SELECT MAX(t2.PriceDate) FROM yourTable t2 WHERE t2.Item = t1.Item);

这比使用 MAX(( 函数更有效:

SELECT Item, Price, PriceDate
FROM customer_pricing t1
WHERE NOT EXITS(
SELECT *
FROM customer_pricing t2
WHERE t1.item = t2.item
AND t1.PriceDate < t2.PriceDate
)

解决方案是 @dewey 并且是

SELECT t1.item, t1.unit_price1, t1.effect_date, t1.site_ref
FROM dbo_itemprice_mst_all AS t1
WHERE (((t1.site_ref)="MED" Or (t1.site_ref)="CRD") AND ((Exists (SELECT *
FROM dbo_itemprice_mst_all t2
WHERE t1.item = t2.item
AND t1.effect_date < t2.effect_date
))=False))
ORDER BY t1.item;

最新更新