我需要从包含以下列"responsable"
,"fecha_contratado"
的表中提取以下数据:
ORIGIN:
fecha_contratado || responsable
"2016-08-04";"sonia"
"2016-05-09";"mercedes"
"2016-03-01";"rebeca"
"2017-02-20";"rebeca"
"2017-01-02";"julia"
"2016-01-11";"anamgarcia"
"2016-06-20";"rebeca"
"2017-01-16";"julia"
"2016-09-26";"sonia"
"2017-03-06";"victoria"
"2016-09-28";"daniel"
"2016-01-07";"emilio"
"2016-02-08";"valle"
"2016-01-14";"mercedes"
"2016-11-14";"mercedes"
"2017-03-09";"alba"
对于每个“负责任的”(负责人)和一年中包含以下数据的行:
Anno | Responsible | January | February | .... | total
"anno": year
"responsable" : responsible
"Enero"(January): rows that are for that year, January and responsible in question (count)
February: same as January but with February month.
March --- December .: equal
Total: total of the year, for that year and responsible (count)
anno | responsable | Enero --- Diciebre | Total
2017;"alba";0;1;0;0;0;0;0;0;0;0;0;0;1
2017;"mercedes";0;0;1;0;0;0;0;0;0;0;0;0;1
2016;"alba";0;0;2;0;0;0;0;0;0;0;0;0;2
现在我明白了,但我一年又负责任,我得到的不止一排,而且我想要一年只有一行而且负责任,具体
select
anno,
tecnico_rrhh,
sum(case when mes = 1 then total else 0 end) as enero,
sum(case when mes = 2 then total else 0 end) as febrero,
sum(case when mes = 3 then total else 0 end) as marzo,
sum(case when mes = 4 then total else 0 end) as abril,
sum(case when mes = 5 then total else 0 end) as mayo,
sum(case when mes = 6 then total else 0 end) as junio,
sum(case when mes = 7 then total else 0 end) as julio,
sum(case when mes = 8 then total else 0 end) as agosto,
sum(case when mes = 9 then total else 0 end) as septiembre,
sum(case when mes = 10 then total else 0 end) as octubre,
sum(case when mes = 11 then total else 0 end) as noviembre,
sum(case when mes = 12 then total else 0 end) as diciembre,
sum(coalesce(total,0)) as total
from (
select
empleado.fecha_contratado as alta,
extract(month from empleado.fecha_contratado) as mes,
extract(year from empleado.fecha_contratado) as anno,
count(1) as total,
usuario_responsable.username as tecnico_rrhh
from rrhh.empleado as empleado
LEFT JOIN commons.usuario as usuario_responsable
on empleado.responsable = usuario_responsable.id
where usuario_responsable.username is not null
group by mes,empleado.fecha_contratado, tecnico_rrhh) altas
group by alta,anno, tecnico_rrhh
order by tecnico_rrhh;
我最接近的是以下查询,但是当我需要为每个“负责任的一年”拉出一行时,它会为相同的“负责”和“年份”重复行。
有人可以帮助我吗? 非常感谢你。
答案 0 :(得分:0)
尝试汇总你的集合:
with p as (
select
anno,
tecnico_rrhh,
sum(case when mes = 1 then total else 0 end) as enero,
sum(case when mes = 2 then total else 0 end) as febrero,
sum(case when mes = 3 then total else 0 end) as marzo,
sum(case when mes = 4 then total else 0 end) as abril,
sum(case when mes = 5 then total else 0 end) as mayo,
sum(case when mes = 6 then total else 0 end) as junio,
sum(case when mes = 7 then total else 0 end) as julio,
sum(case when mes = 8 then total else 0 end) as agosto,
sum(case when mes = 9 then total else 0 end) as septiembre,
sum(case when mes = 10 then total else 0 end) as octubre,
sum(case when mes = 11 then total else 0 end) as noviembre,
sum(case when mes = 12 then total else 0 end) as diciembre,
sum(coalesce(total,0)) as total
from (
select
empleado.fecha_contratado as alta,
extract(month from empleado.fecha_contratado) as mes,
extract(year from empleado.fecha_contratado) as anno,
count(1) as total,
usuario_responsable.username as tecnico_rrhh
from rrhh.empleado as empleado
LEFT JOIN commons.usuario as usuario_responsable
on empleado.responsable = usuario_responsable.id
where usuario_responsable.username is not null
group by mes,empleado.fecha_contratado, tecnico_rrhh) altas
group by alta,anno, tecnico_rrhh
order by tecnico_rrhh
)
select anno, tecnico_rrhh, sum(enero), sum(febrero), sum(marzo), sum(abril), sum(mayo), sum(junio), sum(julio), sum(agosto), sum(septiembre), sum(octubre), sum(noviembre), sum(diciembre)
from p
group by anno, tecnico_rrhh;