分组依据内部分组依据?

group by inside group by?

假设我有 emp_no、year_that_got_paid、性别、amount_paid 我想按 year_that_got_paid 和性别分组,这样我就可以看到每年(1990 年、1991 年……)每个性别有多少人获得报酬。但是如果同一个emp_no在同一年出现两次,就算多了一个同性别的人,其实是一样的

注意:我简化了问题的 table,但它实际上有更多的列,而且主键无助于防止当年的重复员工 ID。

因此,我想,也许是一种

select count(*) 
from tableX 
group by year_that_got_paid, gender

但是我需要第二组或某种“不同的”来每 emp_no、year_that_got_paid 只计数一次。如果我按 3 个字段分组,它也不起作用,因为它会增加很多行,说 1、2 或员工当年收到薪水的次数。

只需对员工 ID 执行 COUNT DISTINCT

您的 table 存储人们赚取的钱。人们可以转换性别,因此您可以存储性别。而且他们一年可以赚很多钱。

因此,如果 Bobby (Roberta / Robert) 在 2020 年作为女性赚取 1000 美元,作为男性赚取 500 美元,作为女性赚取 500 美元,您希望这在 2020 年算作 1 名男性(已经赚取500 美元)和一名女性(已赚取 1500 美元)。

为了实现这一点,先按一年内的员工人数和性别汇总:

select
  year_that_got_paid,
  count(case when gender = 'male' then 1 end) as male_count,
  count(case when gender = 'female' then 1 end) as female_count,
  sum(case when gender = 'male' then sum_amount_paid else 0 end) as male_paid,
  sum(case when gender = 'female' then sum_amount_paid else 0 end) as female_paid
from
(
  select year_that_got_paid, emp_id, gender, sum(amount_paid) as sum_amount_paid
  from transgender_payments
  group by year_that_got_paid, emp_id, gender
) per_emp_and_their_gender
group by year_that_got_paid
order by year_that_got_paid;