select * from 表 A where invoice_id =(select top 1 invoice_id from 表 where user_id=A.user_id)

解决方案 »

  1.   

    declare @tb table(invoice_id int,user_id int)
    insert @tb
    select 656789,9097 union all
    select 656756,9097 union all
    select 656555,9097 union all
    select 676549,9097 union all
    select 700128,8423 union all
    select 718901,98526 union all
    select 715092,98526 union all
    select 448926,2751  union all
    select 517855,2751  union all
    select 485157,2751 select * from @tb A where invoice_id =(select top 1 invoice_id from @tb where user_id=A.user_id)--结果
    /*invoice_id  user_id     
    ----------- ----------- 
    656789      9097
    700128      8423
    718901      98526
    448926      2751(所影响的行数为 4 行)
    */
      

  2.   

    如果是按invoice_id排序,然后在提取第一行呢?
      

  3.   

    你试一下:select * from test where invoice_id + '-' + [user_id] in 
    (select max(invoice_id) + '-' + [userid] from test Group by [user_id])