select * from tb a where not exists(select 1 from tb where 单据号=a.单据号 and 条码=a.条码 and 日期>a.日期)

解决方案 »

  1.   

    这个问题出现很多次了 
    sdhdy帮忙总结下 解释下 
      

  2.   

    if object_id('[tb]') is not null drop table [tb] 
    go
    create table tb([单据号] varchar(20),[日期] varchar(10),[条码] varchar(10),[进价/付款] varchar(10),[别] varchar(10),[序号] varchar(10))
    insert tb select 'SD200901011','2009-04-10','A0001',190,1,1
    insert tb select 'SD200901011','2009-04-10','A0001',192,2,2
    insert tb select 'SD200901011','2009-04-10','A0002',193,1,4
    insert tb select 'SD200901011','2009-04-10','A0002',194,1,5
    insert tb select 'SD200901013','2009-04-12','A0001',190,1,1
    insert tb select 'SD200901013','2009-04-12','A0001',192,2,2
    insert tb select 'SD200901013','2009-04-12','A0002',193,1,4
    insert tb select 'SD200901013','2009-04-12','A0002',194,1,5
    select * from tb a where not exists(select 1 from tb where 条码=a.条码 and 日期>a.日期 or (条码=a.条码 and [进价/付款]>a.[进价/付款]))
    /*
    单据号                  日期         条码         进价/付款      别          序号
    -------------------- ---------- ---------- ---------- ---------- ----------
    SD200901013          2009-04-12 A0001      192        2          2
    SD200901013          2009-04-12 A0002      194        1          5(2 行受影响)
    */