SQL查询max(), count()

发布于 2024-12-11 03:01:07 字数 342 浏览 0 评论 0原文

数据库模式看起来像

员工(员工名称,街道,城市)
作品(员工姓名、公司名称、工资)
公司(公司名称,城市)
管理(员工姓名,经理姓名)

需要执行的查询是:
找到拥有最多员工的公司。

我可以通过查询找到最大计数:

SELECT max( cnt ) max_cnt
FROM (

SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
)w1;

但是现在我找不到公司的名称。如果有人有任何想法请分享。

the database schema looks like

employee(employee_name,street,city)
works(employee_name,company_name,salary)
company(company_name,city)
manages(employee_name,manager_name)

the query needed to do is:
find the company that has the most employees.

I could find out the maximum count by the query:

SELECT max( cnt ) max_cnt
FROM (

SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
)w1;

But now I can't find out the name of the company. If anyone has some idea please share.

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

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

发布评论

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

评论(8

淡淡的优雅 2024-12-18 03:01:07

要获取包含最大值的整行,可以使用 ORDER BY ... DESC LIMIT 1 而不是 MAX

SELECT company_name, cnt
FROM (
    SELECT company_name, count(employee_name) AS cnt
    FROM works
    GROUP BY company_name
) w1
ORDER BY cnt DESC
LIMIT 1

To get the entire row containing the maximum value you can use ORDER BY ... DESC LIMIT 1 instead of MAX:

SELECT company_name, cnt
FROM (
    SELECT company_name, count(employee_name) AS cnt
    FROM works
    GROUP BY company_name
) w1
ORDER BY cnt DESC
LIMIT 1
九厘米的零° 2024-12-18 03:01:07

怎么样:

SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
ORDER BY cnt DESC
LIMIT 1;

编辑:

上面针对 MySQL 进行了更正

How about something like:

SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
ORDER BY cnt DESC
LIMIT 1;

Edit:

Corrected above for MySQL

黎夕旧梦 2024-12-18 03:01:07
SELECT company_name,count(*) as cnt 
FROM works 
GROUP BY company_name 
ORDER BY cnt DESC
SELECT company_name,count(*) as cnt 
FROM works 
GROUP BY company_name 
ORDER BY cnt DESC
唐婉 2024-12-18 03:01:07
select company_name 
from works
group by company_name
having count(distinct employee_name)>=all(select count(distinct employee_name)
from works
group by company_name )
select company_name 
from works
group by company_name
having count(distinct employee_name)>=all(select count(distinct employee_name)
from works
group by company_name )
小糖芽 2024-12-18 03:01:07

这是工作查询

Select * from(SELECT count(EmpName)cnt, CName FROM works GROUP BY CName Order By cnt desc) where ROWNUM = 1;

Here's the working query

Select * from(SELECT count(EmpName)cnt, CName FROM works GROUP BY CName Order By cnt desc) where ROWNUM = 1;
≈。彩虹 2024-12-18 03:01:07

这看起来像是一个课程问题。

如果不止一家公司拥有相同的最大员工人数,则使用 LIMIT 进行的查询将不起作用。 “ORDER BY”没有过滤掉无用的信息。因此我们有以下解决方案

SELECT company_name FROM
(SELECT company_name, count(employee_name) cnt
    FROM works
    GROUP BY company_name) 
JOIN 
(SELECT max(cnt) max_cnt
FROM (
    SELECT count(employee_name) cnt
    FROM works
    GROUP BY company_name
)) ON cnt = max_cnt

This looks like a course question.

If more than one companies have the same largest number of employees the query with LIMIT doesn't work. "ORDER BY" didn't filter out useless info. Thus we have the following solution

SELECT company_name FROM
(SELECT company_name, count(employee_name) cnt
    FROM works
    GROUP BY company_name) 
JOIN 
(SELECT max(cnt) max_cnt
FROM (
    SELECT count(employee_name) cnt
    FROM works
    GROUP BY company_name
)) ON cnt = max_cnt
枫以 2024-12-18 03:01:07
select company_name from works_for
group by company_name
having count(employee_name) = (select max(count(employee_name))from works_for
group by company_name);
select company_name from works_for
group by company_name
having count(employee_name) = (select max(count(employee_name))from works_for
group by company_name);
晌融 2024-12-18 03:01:07

在甲骨文中

    select company_name, count(*) as count 
    from works 
    group by company_name
    having count(*) >= all(select count(*) from works group by company_name)

In Oracle

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