表table:
f_name f_price f_date
1       20       2010-12-03 
2       30       2010-12-03 
1       40       2010-12-04 
4       45       2010-12-04
请问如何得出f_name f_price f_date
2       30       2010-12-03 
1       40       2010-12-04 
4       45       2010-12-04即求f_name 各自最大日期的行

解决方案 »

  1.   


    create table table1
    (
    f_name int,
    f_price int,
    f_date date
    )
    go
    insert into table1
    select 1, 20, '2010-12-03'
    union all select 2, 30, '2010-12-03'  
    union all select 1, 40, '2010-12-04'  
    union all select 4, 45, '2010-12-04'
    go
    --
    with temp
    as
    (
    select *,n=ROW_NUMBER()over(order by f_date) from table1
    )
    select * from temp where n in(
    select max(n) from temp
    group by f_name
    )
    --
    select * from table1 
    where cast(f_name as varchar(10))+convert(varchar(10),f_date,120) in
    (select cast(f_name as varchar(10))+convert(varchar(10),max(f_date),120) nt from table1
    group by f_name)
      

  2.   

    select *
    from tb a
    where not exists(select 1 from tb b where a.f_name<b.f_name and a.f_price=b.f_price)
      

  3.   


    with cte as
    (
    select f_name,max_f_date=max(f_date) from table group by f_name
    )
    select t2.f_name,t1.f_price,t2.max_f_date from table as t1,cte as t2 where t2.max_f_date=t1.f_date and t2.f_name=t1.f_name order by t1.f_name---经测试可以得出楼主要求的结果