TAB1
T_NO  Goods_Code   Letter_No    Goods_DateTiem
1234   R            P1012007      2008-01-02 10:11
1235   R            P1012007       2008-01-02 10:11
1234   C            P1012007       2008-01-02  12:11需要查询结果如下T_NO  Goods_Code   Letter_No    Goods_DateTiem
1234    C           P1012007   2008-01-02 12:11
1235    R           P1012007   2008-01-02 10:11表中,P1012007本来有二条记录,现在我只对1234进行了处理,1235的状态还是R

解决方案 »

  1.   

    select * from tab1 a where not exists
    (select 1 from tab1 where goods_code=a.goods_code and t_no>a.t_no)
      

  2.   


    这样?
    select * from tab1 a where not exists(select 1 from tab1 where T_NO=a.T_NO and Goods_DateTiem>a.Goods_DateTiem)
      

  3.   

    不行啊,
    select * from tab1 a where not exists(select 1 from tab1 where T_NO=a.T_NO and Goods_DateTiem>a.Goods_DateTiem)
    这样查询把Letter_No不是P1012007 的也查询出来,我加了Letter_No只有一条记录了
      

  4.   


    那这样?还不是太明白楼主的需求:
    select   *   from   tab1   a   where Letter_No='P1012007' and not   exists(select   1   from   tab1   where Letter_No='P1012007' and  T_NO=a.T_NO   and   Goods_DateTiem> a.Goods_DateTiem) 
      

  5.   

    select * from T a where not exists(select 1 from t where Letter_No=a.Letter_No and Goods_Code=a.Goods_Code and T_NO>a.T_NO)
      

  6.   


    select * from T a where not exists(select 1 from t where Letter_No=a.Letter_No and Goods_Code=a.Goods_Code and T_NO>a.T_NO) and Letter_No='P1012007'--取某一条
      

  7.   

    select distinct T_NO , Goods_Code , Letter_No, Goods_DateTiem  from tab1
      

  8.   

    select * from t as t1,t as t2 where t1.a=t2.a and t1.b<>t2.b
    LZ本意如此吧?
      

  9.   

    if object_id('tb') is not null
       drop table tb
    go
    create table tb(T_NO int,Goods_Code varchar(1),Letter_No varchar(10),Goods_DateTiem datetime)
    insert tb
    select 1234,'R','P1012007','2008-01-02 10:11' union all
    select 1235,'R','P1012007','2008-01-02 10:11' union all
    select 1234,'C','P1012007','2008-01-02 12:11' select * from tb a 
    where not exists (select 1 from tb where tb.T_NO=a.T_NO and tb.Goods_DateTiem>a.Goods_DateTiem)
    order by a.T_NO
      

  10.   

    select * from TAB1 where T_NO in (
    select max(T_NO) from TAB1 group by Goods_Code , Letter_No , Goods_DateTiem
    ) order by T_NO 
      

  11.   

    加个条件:select * from TAB1 where T_NO in (
    select max(T_NO) from TAB1 group by Goods_Code , Letter_No , Goods_DateTiem 
    where Letter_No = 'P1012007'
    ) order by T_NO 
      

  12.   

    [code=SQL][select * from tb where T_NO+Goods_DateTiem in
    (select T_NO+max(Goods_DateTiem) from tb
     group by T_NO)
    ]
      

  13.   


    select * from tb where T_NO+Goods_DateTiem in
    (select T_NO+max(Goods_DateTiem) from tb
     group by T_NO)