如题.
涉及字段:
商品名称 销售日期 销售价格
a       07-01    10
b       07-02    12
c       07-05    13
a       07-05    8
b       07-06    7
c       07-04    13
得到最近一次的销售价格:结果:
商品名称 销售日期 销售价格
a       07-05    8
b       07-06    7
c       07-05    13
如何实现?请指点. 

解决方案 »

  1.   

    select * from T a where not exists(select 1 from t where 商品名称=a.商品名称 and 销售日期 >a.销售日期 )
      

  2.   

    其它參照 
    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html
      

  3.   

    select a.* from tb a
    where not exists(select 1 from tb where 商品名称=a.商品名称 and 销售日期>a.销售日期)更多写法参见
    http://topic.csdn.net/u/20080123/18/9731d130-0d4b-4c11-8d89-f2c3ca331f0c.html
      

  4.   

    我写详细一些:
    select p.id,max(销售日期) from a left join b on a.id=b.id--表a中并没有销售日期这个字段,通过和b关联才得到这个字段,所以我别搞糊涂了.
      

  5.   

    select * from tb where 销售日期 in(select max(销售日期) from tc)
      

  6.   

    --测试数据--
    declare @tab table(sname varchar(2),sdate varchar(10),jg int)
    insert @tab
    select 'a','07-01',10 union
    select 'b','07-02',12 union
    select 'c','07-05',13 union
    select 'a','07-05',8 union
    select 'b','07-06',7 union
    select 'c','07-04',13;
    --select * from @tab--测试代码--
    select * from @tab
    where sdate in(select max(sdate) from @tab group by sname)
    order by sname
      

  7.   

    --测试数据--
    declare @tab table(sname varchar(2),sdate varchar(10),jg int)
    insert @tab
    select 'a','07-01',10 union
    select 'b','07-02',12 union
    select 'c','07-05',13 union
    select 'a','07-05',8 union
    select 'b','07-06',7 union
    select 'c','07-04',13;
    --select * from @tab--测试代码--
    select * from @tab t where sdate in
    (select top 1 sdate from @tab where sname=t.sname order by sdate desc)
      

  8.   

    select * from T a where not exists(select 1 from t where 商品名称=a.商品名称 and 销售日期 >a.销售日期 )