计数不起作用



不知道我今天早上做了什么,但我一定很用力地撞到了我的头

这是我的查询

select c.id as cid,c.studentid as cstudentid              
from certification c
group by c.id,c.studentid
order by c.studentid

我的结果是这样的

cid studentid
35267  6400
5332   6401
35271  6402
144024 6402
252727 6402
434317 6402
529734 6405
...

我想要像 6402 一样被替换两次以上的学生证

我尝试了这样的基本查询

select c.id as cid,c.studentid as cstudentid              
from certification c
group by c.id,c.studentid
having count(c.studentid) > 2
order by c.studentid

输出为空/无/无表...

系统:邮政9.3

我做错了什么?

您的查询不符合您的期望。它返回每行计数为 1 的所有行。因为您已经对两列进行了分组。此外,它找不到要聚合的相关行。因此,您可以更改查询以按一列 (c.studentId( 对其进行分组,并在另一列 (c.id( 上聚合。它可能像下面的代码:

select count(c.id), c.studentid as cstudentid              
from certification c
group by c.studentid
having count(c.id) > 2
order by c.studentid

如果需要复制的c.id,则应编写 CTE 查询。这篇文章的第二个答案将帮助你。

试试这个: 但是,您应该使用输出指定: 从分组中删除 cid 作为,从您的表格中似乎 cid 是唯一的

select c.studentid as cstudentid              
from certification c
group by c.studentid
having count(c.studentid) > 2
order by c.studentid

最新更新