窗口功能所需的组?

发布于 2025-01-23 13:03:44 字数 506 浏览 0 评论 0 原文

我有员工表:

id  int
first_name varchar
last_name  varchar
department_id  int
department_name  varchar
position  varchar

我想按大小对部门进行排名。这起作用:

select
department_id d_id,
rank() over (order by count(*) desc) r
from employees
group by department_id

我不明白的是为什么需要组。如果我删除它,我会收到此错误:

column "employee.department_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: department_id d_id,

I have this schema for employee table:

id  int
first_name varchar
last_name  varchar
department_id  int
department_name  varchar
position  varchar

I want to rank departments by size. This works:

select
department_id d_id,
rank() over (order by count(*) desc) r
from employees
group by department_id

What I don't understand is why group by is required. If I remove it I get this error:

column "employee.department_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: department_id d_id,

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

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

发布评论

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

评论(2

风筝在阴天搁浅。 2025-01-30 13:03:44

此查询:

select department_id d_id, 
       count(*) number_of_employees
from az_employees
group by department_id

为每个部门的员工人数返回1行。

您的查询使用 rank()窗口函数以根据汇总功能的结果对部门进行排名 count(*)

rank() over (order by count(*) desc) r

rank()在汇总查询的结果上操作(每个部门的1行带有2列: dectment> dectmence_id and code> and >计数(*))并为每个部门返回1列。

与使用汇总查询作为子查询相同:

select d_id, rank() over (order by number_of_employees desc) r
from (
  select department_id d_id, 
         count(*) number_of_employees
  from az_employees
  group by department_id 
) t

但是您的查询更简单。

This query:

select department_id d_id, 
       count(*) number_of_employees
from az_employees
group by department_id

returns 1 row for each department with the number of employees in the department.

Your query uses RANK() window function to rank the departments based on the results of the aggregate function count(*):

rank() over (order by count(*) desc) r

RANK() operates on the results of the aggregate query (1 row for each department with 2 columns: department_id and count(*)) and returns 1 more column for each department.

It would be the same as if you used the aggregate query as a subquery:

select d_id, rank() over (order by number_of_employees desc) r
from (
  select department_id d_id, 
         count(*) number_of_employees
  from az_employees
  group by department_id 
) t

but your query is simpler.

何其悲哀 2025-01-30 13:03:44

这是对另一个问题的答案,但可能是您真正想要的。

select
department_id d_id,
count(*) count // this line can be removed
from az_employees
group by department_id
order by count(*)
having count(*) > 0 // this line can be removed, but if you for instance change zero to one one departments with more than one employee will be shown.

正如上一个答案所解释的那样,组用于聚合。因此,在我不知道这种情况的同时,没有理由详细介绍详细

信息,除非从中获得一些收益,否则通常是一个不好的习惯与原始值一样容易。
但是在某些情况下,等级是一个很好的解决方案,但我认为这不是其中之一。

This is sort of an answer to another question but might be what you really want.

select
department_id d_id,
count(*) count // this line can be removed
from az_employees
group by department_id
order by count(*)
having count(*) > 0 // this line can be removed, but if you for instance change zero to one one departments with more than one employee will be shown.

as previous answer explained, group by is used to aggregate. So no reason to go into details

While I don't know this scenario it's generally a bad habit to remove original data unless there is some gain from it, and as rank is usually only used to sort by later on and that can be done just as easily with the original value.
But there is of-course situations when rank is a good solution, but I don't think this is one of those.

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