限制输出 group-by sql 查询中出现的次数

发布于 2024-11-29 12:43:14 字数 740 浏览 0 评论 0原文

我有这个查询,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏璃陌 2024-12-06 12:43:14

我将使用子查询来获取像这样的非唯一公司名称。

select rep, companyname,count(companyname) as [count], Commission from customers
where companyname in (
  select c1.companyname from customers c1
  group by c1.companyname having count(*) >= 2
)
group by repid,companyname,Commission

I would use a subquery to get the non-unique company names like this.

select rep, companyname,count(companyname) as [count], Commission from customers
where companyname in (
  select c1.companyname from customers c1
  group by c1.companyname having count(*) >= 2
)
group by repid,companyname,Commission
凉城凉梦凉人心 2024-12-06 12:43:14

我想这会符合你的要求。我想不出没有某种子查询或 CTE 的方法:

select 
    rep, companyname, [count], commission
from (
    select 
        rep, companyname,count(companyname) as [count], Commission,
        count(1) over (PARTITION by companyname) as [companycount]
    from customers
    group by repid,companyname,Commission
) sub
where companycount > 1

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:

select 
    rep, companyname, [count], commission
from (
    select 
        rep, companyname,count(companyname) as [count], Commission,
        count(1) over (PARTITION by companyname) as [companycount]
    from customers
    group by repid,companyname,Commission
) sub
where companycount > 1
浊酒尽余欢 2024-12-06 12:43:14
select rep
     , companyname
     , count(*) as [count]                  --- equivalent to count(companyname)
     , Commission
from customers c
where exists
      ( select *
        from customers c2
        where c2.companyname = c.companyname 
          and ( c2.repid <> c.repid
             or c2.Commission <> c.Commission 
              )
          and ( extra-conditions )
      )
  and ( extra-conditions )
group by repid, companyname, Commission
select rep
     , companyname
     , count(*) as [count]                  --- equivalent to count(companyname)
     , Commission
from customers c
where exists
      ( select *
        from customers c2
        where c2.companyname = c.companyname 
          and ( c2.repid <> c.repid
             or c2.Commission <> c.Commission 
              )
          and ( extra-conditions )
      )
  and ( extra-conditions )
group by repid, companyname, Commission
娇妻 2024-12-06 12:43:14

在分组依据后添加 HAVING 子句,例如 HAVING count(companyName) > 1

Add a HAVING clause after your group by, e.g. HAVING count(companyName) > 1

左岸枫 2024-12-06 12:43:14

您正在寻找 HAVING 关键字,它本质上是 GROUP BYWHERE 条件

select rep, companyname,count(companyname) as [count], Commission from customers
group by repid,companyname,Commission
having count(companyname) > 1

You're looking for the HAVING keyword, which is essentially a WHERE condition for your GROUP BY

select rep, companyname,count(companyname) as [count], Commission from customers
group by repid,companyname,Commission
having count(companyname) > 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文