表: 商品(商品名, 价格, 日期)数据比如:
大豆  4元   2009-1-5
大豆  4.4元   2009-1-4
玉米  3元    2009-1-27要sql查出2种商品最近的价格和日期我用distinct搞不定啊 日期出不来

解决方案 »

  1.   

    select * 
    from tb a 
    where 日期 in(select max(日期) from tb b where a.商品名=b.商品名);
      

  2.   

    或者
    select a.*
    from tb a
    inner join (select 商品名,max(日期) 日期 from tb group by 商品名) b
    on a.商品名=b.商品名 and a.日期=b.日期;
      

  3.   

    试试:
    select 商品名, 价格,日期 from 商品 where (商品名,日期) in
    (
        select 商品名,max(日期) from 商品
    )
      

  4.   

    select 名称,价格,日期 from 
    (select 名称,价格,日期,rownumber() over(partition by 名称 order by 名称,日期 desc) cn from table) 
    where cn=1
      

  5.   

    SQL> select name,price,time from (select a.*,rank()over(partition by name order by time desc) rn from goods a) where rn=1;
     
    NAME                 PRICE                TIME
    -------------------- -------------------- --------------------
    大豆                 4元                  2009-1-5
    玉米                 3元                  2009-1-27