数据库表结构如下
supplierCode goodsCode unitPrice lastTime
供应商代码 物品代码 物品单价 更新时间
更新一次供应商供应的物品价格就插入一条数据。
如:
GY00001 WP00001 30 2015-05-19 15:16:01.000
GY00001 WP00001 50 2015-05-25 20:16:01.000
GY00001 对应的WP00001 物品 5月25日更新了一次价格
我现在要得到所有的supplierCode和goodsCode 对应的最近的时间的unitPrice
也就是说supplierCode 和goodsCode相同的记录只出现一次,这条记录的lastTime取最近的一条,unitPrice取最近这条lastTime对应的unitPrice
------解决思路----------------------
select * from table_name a
where
(select count(*) from table_name b where a.supplierCode = b.supplierCode and a.goodsCode = b.goodsCode and a.lastTime <= b.lastTime) <=1
order by a.lastTime desc