2017-04-21 8 views
0

ich aus einer Tabelle extrahieren müssen, die die folgenden Spalten "responsable", "fecha_contratado" die folgenden Daten:Monat und Jahr ab dem Datum Postgresql

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" 

Für jede „responsable“ (verantwortliche Person) und Jahr eine Reihe mit der folgende Daten:

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 

Jetzt bekomme ich das, aber ich für ein Jahr und verantwortlich ich mehr als eine Zeile, und ich möchte nur eine Zeile für ein Jahr und verantwortlich, Beton

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; 

Das nächste, was ich bekommen muss, ist die folgende Abfrage, aber es wiederholt Zeilen für die gleiche "verantwortlich" und "Jahr", wenn ich eine Zeile für jedes "verantwortliche Jahr" ziehen muss.

Kann mir jemand helfen. Vielen Dank.

+0

Ich bin mir nicht sicher, was der Datensatz Sie haben, aber von 'sonst 0 'Ich nehme an, Sie können' Summe (enero), '... so weiter über Zeilen, um eine Zeile zu setzen? .. –

+0

Können Sie Ihre Frage bearbeiten und erwartete Ausgabe von Ihrem Tabellenexemplar hinzufügen –

Antwort

0

bereites Set Aggregieren:

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; 
Verwandte Themen