有以下数据

ty   tyname    人员    数量
a a1 me 3
a a2 me 1
b b1 you 1
b b2 you 2
b b3 you 1
···
···
···要求得到以下结果:(即是按人员分组,每人只读取数量最大的那条记录
ty   tyname    人员    数量
a a1 me 3
b b2 you 2
···
···
···

解决方案 »

  1.   

    selecr ty,tyname,人员,max(数量) 数量 from tb group by ty,tyname,人员
      

  2.   

    select ty,tyname,人员,max(数量) 数量 from tb group by ty,tyname,人员
      

  3.   


    group by ty,tyname,人员,出来的结果肯定不对的拉。要求是按人员分组,每人只读取数量最大的那条记录
      

  4.   

    select * from tb a
     where not exsits(select 1 from tb where 人员=a.人员 and 数量>a.数量)
      

  5.   

    select * from tb a
     where not exists(select 1 from tb where 人员=a.人员 and 数量>a.数量)
    --or
    select * from tb a
     where 数量=(select max(数量) from tb where 人员=a.人员)
      

  6.   


    use tempdb;
    /*
    create table tb
    (
    ty nvarchar(10) not null,
    tyname nvarchar(10) not null,
    人员 nvarchar(10) not null,
    数量 int not null
    );
    insert into tb(ty,tyname,人员,数量)
    values
    ('a','a1','me',3),
    ('a','a2','me',1),
    ('b','b1','you',1),
    ('b','b2','you',2),
    ('b','b3','you',1);
    */
    select t.ty,t.tyname,t.人员,t.数量
    from
    (
    select *,
    ROW_NUMBER() over(partition by tb.ty order by tb.数量 desc) as [sort]
    from tb
    ) as t
    where t.sort = 1;
      

  7.   

    select * from tb a where 数量=(select max(数量) from tb where 人员=a.人员) order by ty
     
      

  8.   

    select t.ty,t.tyname,t.人员,t.数量
    from
    (
    select *,
    ROW_NUMBER() over(partition by tb.人员 order by tb.数量 desc) as [sort]
    from tb
    ) as t
    where t.sort = 1;