有下面一张表记录物料的价格:表名(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

解决方案 »

  1.   

    select top 1 * from table1 where ModiDate< 1980-01-01 order by ModiDate
      

  2.   

    select yourtable.* from yourtable, 
    ( 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
      

  3.   

    by the way, you can use dbparameter as follows:
    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
      

  4.   

    select 物料号,价格,max(日期) from 物料价格 where 日期 < @日期
    group by 物料号, 价格
      

  5.   

    redwolf_123(雨恨云愁) ( ) 信誉:100    Blog  2006-09-01 12:37:00  得分: 0  
     
     
       一楼那个应该错了。。 如果一次查出来还真有难度
    不过分几步查的话倒是可能。。--------------------------

    我把“最近一次”理解错了