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

Sql server 如何使用group by查找正在sql server中注册2个或更多课程的所有学生的o/p?,sql-server,group-by,count,having,Sql Server,Group By,Count,Having,我有一个表名为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和stack

我有一个表名为
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新手需要帮助

尝试


你试过自己解决这个问题吗?这是一个非常常见的家庭作业。来自:请求家庭作业帮助的问题必须包括到目前为止您为解决问题所做工作的摘要,以及您解决问题的困难的描述。Write Threaded@Timbiegeleisen您在问题标签中有“having”。你试过在查询中使用它吗?对不起,我忘了现在使用它,但仍然没有得到所需的o/p
select sid,COUNT(*),cid from testtabl1 group by sid,cid having count(*)>1
SELECT sid as StudentId,
       COUNT(cid) as SelectedCoursesCount
FROM testtabl1
GROUP BY sid
HAVING COUNT(cid) > 1;