【发布时间】:2021-10-13 22:28:41
【问题描述】:
这次我在 PostgreSQL 数据库上有一个表,其中包含员工姓名、他开始工作的日期和他离开公司的日期,在员工仍然留在公司的情况下,该字段为空价值。 知道了这一点,我想知道有多少人在预定日期工作,例如:
我想知道 2021 年 1 月有多少人在公司工作。
我不知道从哪里开始,在某些尝试中,我得到了每月的招聘和裁员数量,但我需要在另一列中显示每月的累计值。
我希望我自己理解了,我将把我得到的最后一条 SQL 留在这里。
select reference, sum(hires) from
(
select
date_trunc('month', date_hires) as reference,
count(*) as hires
from
ponto_mais_relatorio_colaboradores
group by
date_hires
union all
select
date_trunc('month', date_layoff) as reference,
count(*)*-1 as layoffs
from
ponto_mais_relatorio_colaboradores
group by
date_layoff
) as reference
join calendar_aux on calendar_aux.ano_mes = reference
group by reference
order by reference
【问题讨论】:
标签: sql postgresql metabase