求一条sql语句 
有4个字段,名称,价格, 日期 ,单位
 
a 1.2 07-6-12 ,d
a 1.2 07-6-12 ,d
a 1.1 07-6-11, a
b 3.5 07-6-10, b如何取到最新的价格和所在的日期和单位  ,如
a 1.2 07-6-12,d
b 3.5 07-6-10 ,b 请注意,相同的日期,相同的名称只要一条就够了
谢谢!

解决方案 »

  1.   

    ----方法1:
    select * from table as a where not exists(select 1 from table where 名称=a.名称 and 日期 > a.日期)----方法2:
    select * from table as a where 日期=(select max(日期) from table where 名称=a.名称)
      

  2.   

    create table #aa( 名称 varchar(5),价格 varchar(5),日期 datetime,单位 varchar(5))
    insert into #aa
    select 'a', '1.2', '07-6-12' ,'d' union all
    select 'a', '1.2', '07-6-12' ,'d' union all
    select 'a', '1.1', '07-6-11' ,'a' union all
    select 'b', '3.1', '07-6-10' ,'b' select distinct * from  #aa a where 
    not exists(select 1 from  #aa b where a.名称=b.名称 and a.日期<日期)drop table #aa
    (所影响的行数为 4 行)名称    价格    日期                                                     单位    
    ----- ----- ------------------------------------------------------ ----- 
    a     1.2   2007-06-12 00:00:00.000                                d
    b     3.1   2007-06-10 00:00:00.000                                b(所影响的行数为 2 行)
      

  3.   

    hellowork(一两清风) 前面加上个distinct就可以了
      

  4.   

    select distinct * from 表 a  (select 名称,max(日期) as 日期 from 表 group by 名称) b
    where a.名称=b.名称 and a.日期=b.日期