有下面一张表记录物料的价格:表名(MaterialPrice)Material Price ModiDate
物料号 价格 修改日期
001 1.00 2005.10.1
002 2.00 2005.10.2
003 1.50 2005.11.3001 1.2 2006.1.1
002 1.8 2006.1.1
003 1.4 2006.3.2请问:
需要提取价格表中小于指定日期的所有物料的最近一次的价格。如:需要获取 小于 2005.10.30 的所有物料的最近一次的价格,
结果:
物料号 价格 修改日期
001 1.00 2005.10.1
002 2.00 2005.10.2如:需要获取 小于 2006.1.30 的所有物料的最近一次的价格,
结果:
001 1.2 2006.1.1
002 1.8 2006.1.1003 1.50 2005.11.3如:需要获取 小于 2006.6.30 的所有物料的最近一次的价格,
结果:
001 1.2 2006.1.1
002 1.8 2006.1.1
003 1.4 2006.3.2
物料号 价格 修改日期
001 1.00 2005.10.1
002 2.00 2005.10.2
003 1.50 2005.11.3001 1.2 2006.1.1
002 1.8 2006.1.1
003 1.4 2006.3.2请问:
需要提取价格表中小于指定日期的所有物料的最近一次的价格。如:需要获取 小于 2005.10.30 的所有物料的最近一次的价格,
结果:
物料号 价格 修改日期
001 1.00 2005.10.1
002 2.00 2005.10.2如:需要获取 小于 2006.1.30 的所有物料的最近一次的价格,
结果:
001 1.2 2006.1.1
002 1.8 2006.1.1003 1.50 2005.11.3如:需要获取 小于 2006.6.30 的所有物料的最近一次的价格,
结果:
001 1.2 2006.1.1
002 1.8 2006.1.1
003 1.4 2006.3.2
( select Material, Max( ModiDate ) as TopDate from yourtable where ModiDate < '2006.6.30' group by Material ) as MaxTable
where yourtable.Material = MaxTable.Material
and yourtable.ModiDate = MaxTable.TopDate
select yourtable.* from yourtable,
( select Material, Max( ModiDate ) as TopDate from yourtable where ModiDate < @EndDate group by Material ) as MaxTable
where yourtable.Material = MaxTable.Material
and yourtable.ModiDate = MaxTable.TopDate
group by 物料号, 价格
一楼那个应该错了。。 如果一次查出来还真有难度
不过分几步查的话倒是可能。。--------------------------
恩
我把“最近一次”理解错了