限制输出 group-by sql 查询中出现的次数
我有这个查询,
select rep, companyname,count(companyname) as [count], Commission from customers
group by repid,companyname,Commission
它返回让我们说
rep companyname count commision
1 ABC 1 10%
2 XYZ 2 10%
2 XYZ 1 20%
3 JKL 4 10%
3 JKL 1 30%
“欲望输出”是
rep companyname count commision
2 XYZ 2 10%
2 XYZ 1 20%
3 JKL 4 10%
3 JKL 1 30%
“我想要一个输出”,以便我只显示结果中重复两次或多次的公司。如何修改上面的查询。我使查询变得简单(删除 where 子句)。
I have this query
select rep, companyname,count(companyname) as [count], Commission from customers
group by repid,companyname,Commission
It returns lets say
rep companyname count commision
1 ABC 1 10%
2 XYZ 2 10%
2 XYZ 1 20%
3 JKL 4 10%
3 JKL 1 30%
Desire output is
rep companyname count commision
2 XYZ 2 10%
2 XYZ 1 20%
3 JKL 4 10%
3 JKL 1 30%
I would like to have an output so that I show the only those companies who are repeated twice or more in the result. How do I modify the above query. I made the query simple (remove where clause).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将使用子查询来获取像这样的非唯一公司名称。
I would use a subquery to get the non-unique company names like this.
我想这会符合你的要求。我想不出没有某种子查询或 CTE 的方法:
I think this will match your requirements. I couldn't think of a way of doing it without some sort of sub query or CTE:
在分组依据后添加 HAVING 子句,例如 HAVING count(companyName) > 1
Add a HAVING clause after your group by, e.g. HAVING count(companyName) > 1
您正在寻找
HAVING
关键字,它本质上是GROUP BY
的WHERE
条件You're looking for the
HAVING
keyword, which is essentially aWHERE
condition for yourGROUP BY