数据表T有下面的数据
AIP      PID
1        2
1        2
2        20
2        40
3        60
4        100现在想选出来,相同AIP,取PID的最大的值,如果PID也相同就随便取一个。结果呈现为下面的AIP      PID
1        2
2        40
3        60
4        100

解决方案 »

  1.   

    select * from tb t
    where not exists(select * from tb where aip=t.aip and pid>t.pid)
      

  2.   

    SELECT AIP,MAX(PID)PID FROM (SELECT DISTINCT * FROM TB)AS T GRUOP BY AIP
      

  3.   

    select AIP,max(PID) as PID from tb group by AIP 
      

  4.   

    楼上的是 PID 取最大。。
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-11-11 14:29:38
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([AIP] int,[PID] int)
    insert [tb]
    select 1,2 union all
    select 1,2 union all
    select 2,20 union all
    select 2,40 union all
    select 3,60 union all
    select 4,100
    --------------开始查询--------------------------select distinct * from [tb] t where PID=(select max(pid) from tb where aip=t.aip) order by 1
    ----------------结果----------------------------
    /* AIP         PID
    ----------- -----------
    1           2
    2           40
    3           60
    4           100(4 行受影响)
    */
      

  6.   

    select aip, max(pid) from tableName group by aip
      

  7.   

    declare @tb table (AIP int,PID int)
    insert into @tb
    select 1,2 union all
    select 1,2 union all
    select 2,20 union all
    select 2,40 union all
    select 3,60 union all
    select 4,100select * ,id=identity(int,1,1) into #t from @tbselect aip,pid from #t t
    where not exists(select * from #t where aip=t.aip and  id>t.id)aip         pid
    ----------- -----------
    1           2
    2           40
    3           60
    4           100(4 行受影响)
    drop table #t
      

  8.   


    SELECT AIP,MAX(PID)PID FROM (SELECT DISTINCT * FROM TB)AS T GRUOP BY AIP
      

  9.   

    select AIP,max(PID) as PID from tb group by AIP
    楼主的意思就是按AIP分组取最大吧.
      

  10.   

    SELECT AIP,MAX(PID)PID FROM (SELECT DISTINCT * FROM TB)AS T GROUP BY AIP晕,好像分组会去重了,
      

  11.   

    select aip, max(pid) from tb group by aip--> 测试数据:
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([AIP] int,[PID] int)
    insert [tb]
    select 1,2 union all
    select 1,2 union all
    select 2,20 union all
    select 2,40 union all
    select 3,60 union all
    select 4,100
    --------------查询SQL--------------------------
    select aip, max(pid) from tb group by aip
      

  12.   

    关于分组最最大的功能,
    网上好像还有很多复杂的写法.
    有的好几层嵌套,
    我都看不明白它的逻辑.
    其实简单的SQL就可以实现
      

  13.   


    select aip,max(PID) as pid from tb group by aip
      

  14.   

    有那么复杂吗?不就一句话
    SELECT AIP, MAX(PID) AS PID FROM T GROUP BY AIP
      

  15.   

    select aip, max(pid) from tableName group by aip
      

  16.   

    数据表T有下面的数据 
    AIP      PID 
    1        2 
    1        2 
    2        20 
    2        40 
    3        60 
    4        100 现在想选出来,相同AIP,取PID的最大的值,如果PID也相同就随便取一个。
    -------------------------------------------------------
    你不就两个字段吗?PID随不随机取,不还都一样?
      

  17.   

    select AIP,max(PID) as PID from tb group by AIP
      

  18.   

    前面那个太简单了来个难的
    数据表T有下面的数据
    AIP      PID   Name
    1        2     王三
    1        2     王三
    2        20    王三
    2        40    王三
    3        60    王三
    4        100   王三
    1        2     李四
    2        20    李四现在想选王三的数据,相同AIP,取PID的最大的值,如果PID也相同就随便取一个。结果呈现为下面的AIP      PID   Name
    1        2     王三
    2        40    王三
    3        60    王三
    4        100   王三
      

  19.   


    select aip, max(pid) as pid, '王三' as name from tb where name='王三' group by aip
      

  20.   


    select aip, max(pid) as pid, '王三' as name from tb where name='王三' group by aip