34,590
社区成员
发帖
与我相关
我的任务
分享
--这有个比较笨的方法,
create table cj(stuName nvarchar(10),KCM nvarchar(10),cj numeric(5,2))
insert into cj select '张三','语文',98 union select '李四','语文',89 union select '王五','语文',67 union select '周攻','语文',56
union select '张三','数学',89 union select '李四','数学',78 union select '王五','数学',90 union select '周攻','数学',87
select stuName from
(
select count(stuName) num,stuName from
(select row_number() over(partition by KCM order by cj desc) rn,stuName,KCM from cj) tt
where rn = 1 or rn = 2
group by stuName
) tt
where num = 2
select distinct stuname from cj a
where not exists(select kcm from cj b where a.stuname=stuname
and (select count(*) from cj where kcm=b.kcm and stuname!=a.stuname and cj>b.cj)>1)
create table cj1(stuName nvarchar(10),KCM nvarchar(10),cj numeric(5,2))
insert into cj1 select '张三','语文',98 union select '李四','语文',89 union select '王五','语文',67 union select '周攻','语文',56
union select '张三','数学',89 union select '李四','数学',78 union select '王五','数学',90 union select '周攻','数学',87
SELECT stuname FROM cj1 a
where cj IN(SELECT TOP 2 cj FROM cj1 WHERE kcm=a.kcm ORDER BY cj desc)
GROUP BY stuname HAVING(count(1)>1)
--result
/*stuname
----------
张三
(所影响的行数为 1 行)*/
create table cj(stuName nvarchar(10),KCM nvarchar(10),cj numeric(5,2))
insert into cj select '张三','语文',98 union select '李四','语文',89 union select '王五','语文',67 union select '周攻','语文',56
union select '张三','数学',89 union select '李四','数学',78 union select '王五','数学',90 union select '周攻','数学',87
go
select stuname from
(select stuName,kcm,(select count(*) from cj where stuname!=a.stuname and kcm=a.kcm and cj>a.cj) cnt from cj a) x
group by stuname having max(cnt)<=1
go
drop table cj
go