现在有表
id               a  b LPOLY_ RPOLY_ LENGTH
1                1   2 0 0 0.00561023142
2                1          2 0 0 0.00561023142
3                3          1 0 0 0.00464502420
4                3          1 0 0 0.00464502420
5                2          4 0 0 0.00309704121
6                2          4 0 0 0.00309704121
7                2          4 0 0 0.00309704121
8                2          4 0 0 0.00309704121当a,b列的值都一样的时候,如id 5-8行的a,b
取出id的值最大和最小的这一行

解决方案 »

  1.   

    select * from table a where not exists (select 1 from table where a=a.a and b=a.b and id>a.id)
    union all 
    select * from table a where not exists (select 1 from table where a=a.a and b=a.b and id<a.id)
      

  2.   

    select * from tbname t where not exists(select 1 from tbname where a=t.a and b=t.b and id>t.id)
    or
    select * from tbname t where not exists(select 1 from tbname where a=t.a and b=t.b and id<t.id)
      

  3.   

    Select x.* from tbl as x inner Join (
    Select top 1 min(ID) as nID,max(ID) as xID, count(1) cn from tbl 
    group by a,b
    having count(1)>1
    Order by cn) as y
    on x.ID=y.nID or x.ID=y.xID
      

  4.   

    --??select *
    from tablename
    where id in
    (
    select min(id) as new
    from tablename
    group by a,b
    union
    select max(id)
    from tablename
    group by a,b)
      

  5.   

    Select x.* from tbl as x inner Join (
    Select top 1 min(ID) as nID,max(ID) as xID, count(1) cn from tbl 
    group by a,b
    having count(1)>1
    Order by cn desc) as y
    on x.ID=y.nID or x.ID=y.xID
      

  6.   

    Declare @t Table (Id Int,A Int,B Int)
    Insert @t Select 1,1,2
    Union all Select 2,1,2
    Union all Select 3,3,1
    Union all Select 4,3,1
    Union all Select 5,2,4
    Union all Select 6,2,4
    Union all Select 7,2,4
    Union all Select 8,2,4
    Select Max(Id) As Id ,A,B
    From @t
    Group By A,B
    Having Count(1)>1
    Union all 
    Select Min(Id) As Id ,A,B
    From @t
    Group By A,B
    Having Count(1)>1