34,590
社区成员
发帖
与我相关
我的任务
分享
select zh,name,flag,sd,zj,row_number() over(partition by zh,name order by flag desc) as rn from tb1from
create table tb1
(
zh int not null,
name nvarchar(10),
flag int not null,
sd int,
zj int
)
insert into tb1
select 1,'zhang',1,23,26
union all
select 1,'zhang',2,26,33
union all
select 1,'zhang',3,33,36
union all
select 2,'li',1,56,89
union all
select 2,'li',2,89,103
union all
select 2,'li',3,103,106
union all
select 2,'li',4,106,108
union all
select 3,'wang',1,225,228
union all
select 3,'wang',2,228,230
select zh,name,flag,sd,zj,sd+zj as summ from (
select zh,name,flag,sd,zj,row_number() over(partition by zh order by flag desc) as rn from tb1) a
where rn=1
select * from Table a where not exists(select 1 from Table where zh=a.zh and flag>a.flag)
select * from Table where id in (select id from Table where flag in (select max(flag) from Table group by zh))
希望对楼主有所启发select * from Table t where flag = (select max(flag) from Table ts where t.zh = ts.zh)