是否可以在没有连接的情况下按计数(不同)进行分组?

发布于 2024-12-17 12:42:27 字数 669 浏览 2 评论 0原文

Table 1 : (Company)

ID Name
1  A
2  B
3  C

每个公司 (pk = ID) 可以拥有一名或多名员工。

Table 2 :  (Employee)  (CompanyID referencing ID)

CompanyID EmpID Name
1         1     Joe
1         2     Doe
1         3     Boe
2         4     Lou
3         5     Su  
3         6     Ram

查询:

select CompanyID, count(*) from Employee group by CompanyID having count(*) > 1; # Lists companies and their counts.

CompanyID count(*)
1         3  
3         2

对于此查询,我只想要一个包含不同 CompanyID 计数的结果。因此,本例中为“2”[公司 A 和 C]。

简而言之,我正在寻找拥有 2 名或更多员工的公司数量。

是否有办法在没有临时表或连接的情况下获得结果?我正在使用MySQL。

Table 1 : (Company)

ID Name
1  A
2  B
3  C

Each company (pk = ID) can have one or more employees.

Table 2 :  (Employee)  (CompanyID referencing ID)

CompanyID EmpID Name
1         1     Joe
1         2     Doe
1         3     Boe
2         4     Lou
3         5     Su  
3         6     Ram

Query :

select CompanyID, count(*) from Employee group by CompanyID having count(*) > 1; # Lists companies and their counts.

CompanyID count(*)
1         3  
3         2

For this query, I want just one result with the count of distinct CompanyIDs. So, '2' in this case [Companies A and C].

In short, I am looking for number of companies with 2 or more employees.

Is there anyway to get the result without a temp table or a join? I am using MySQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

抱着落日 2024-12-24 12:42:27

是:

select count(*) from
(select CompanyID from Employee group by CompanyID having count(*) > 1) v

或对于范围:

select count(*) from
(select CompanyID from Employee group by CompanyID 
 having count(*) >= 5 and count(*) < 10) v

Yes:

select count(*) from
(select CompanyID from Employee group by CompanyID having count(*) > 1) v

or for ranges:

select count(*) from
(select CompanyID from Employee group by CompanyID 
 having count(*) >= 5 and count(*) < 10) v
ま昔日黯然 2024-12-24 12:42:27

是的,可以使用子查询:

SELECT COUNT(*)
FROM
  ( SELECT 1
    FROM Employee 
    GROUP BY CompanyID 
    HAVING COUNT(*) > 1
  ) AS grp

或者:

SELECT COUNT(DISTINCT CompanyID) 
FROM Employee AS e
WHERE EXISTS
      ( SELECT *
        FROM Employee AS e2
        WHERE e2.CompanyID = e.CompanyId
          AND e2.EmpID <> e.EmpID
      )

或者如果 COUNT(*) 很慢,您可以使用 MIN()MAX()

SELECT COUNT(*)
FROM
  ( SELECT 1 
    FROM Employee 
    GROUP BY CompanyID 
    HAVING MAX(EmpID) > MIN(EmpId)
  ) AS grp

Yes, it's possible with subqueries:

SELECT COUNT(*)
FROM
  ( SELECT 1
    FROM Employee 
    GROUP BY CompanyID 
    HAVING COUNT(*) > 1
  ) AS grp

or:

SELECT COUNT(DISTINCT CompanyID) 
FROM Employee AS e
WHERE EXISTS
      ( SELECT *
        FROM Employee AS e2
        WHERE e2.CompanyID = e.CompanyId
          AND e2.EmpID <> e.EmpID
      )

or perhaps if COUNT(*) is slow, you can use MIN() and MAX():

SELECT COUNT(*)
FROM
  ( SELECT 1 
    FROM Employee 
    GROUP BY CompanyID 
    HAVING MAX(EmpID) > MIN(EmpId)
  ) AS grp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文