如何找到o / p使用group by在sql server中注册2门或更多门课程的所有学生?

问题描述 投票:-2回答:1

我现在有一个表名为testtabl1 我想找到所有参加2门或更多课程的学生, 在这个sid是学生id和cid是课程id

表结构

create table testtabl1(Sid int,cid int,years varchar(20))
insert into testtabl1(Sid,cid,years)
select 1,1,'2016'
union all
select 2,2,'2017'
union all
select 1,2,'2017'

新的SQL Server和stackoverflow需要帮助!!

试着

select sid,COUNT(*),cid from testtabl1 group by sid,cid having count(*)>1
sql-server group-by count having
1个回答
0
投票
SELECT sid as StudentId,
       COUNT(cid) as SelectedCoursesCount
FROM testtabl1
GROUP BY sid
HAVING COUNT(cid) > 1;
© www.soinside.com 2019 - 2024. All rights reserved.