Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Can anyone help me with the sum of two tables with a dynamic pivot.


tables:
horas_trabajadas

equ_codigo | tur_codigo | con_fecha | hor_tra_horas

horas_paradas

equ_codigo | tur_codigo | con_fecha | hor_par_hora

Add hours to be the same code and corresponding to the same date and shift.


begin
declare finish int default 0;
declare cdate date;
declare str, str2 varchar(10000) default "select DISTINCT equ_codigo,";

declare curs cursor for SELECT DISTINCT equ_codigo FROM horas_trabajadas INNER JOIN horas_paradas ON horas_trabajadas.con_fecha=horas_paradas.con
WHERE horas_trabajadas.equ_codigo=horas_paradas.equ_codigo between sdate and edate group by con_fecha;
declare continue handler for not found set finish = 1;
open curs;
my_loop:loop
fetch curs into cdate;
if finish = 1 then
leave my_loop;
end if;
set str = concat(str, "sum(case when con_fecha.horas_trabajadas = '",cdate,"' then hor_tra_horas else null end) as `",cdate,"`,");
set str2 = concat(str2, "sum(case when con_fecha.horas_paradas = '",cdate,"' then hor_par_hora else null end) as `",cdate,"`,");
end loop;
close curs;
set str = substr(str,1,char_length(str)-1);
set str2 = substr(str2,1,char_length(str)-1);
set @str = concat(str," from horas_trabajadas
            group by equ_codigo");
set @str2 = concat(str2," from horas_paradas
            group by equ_codigo");

set @query = concat(str, str2);

prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
end


Thanx.
Posted
Updated 23-Oct-14 17:43pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Oct-14 23:44pm    
Excuse me, where is your question? Help with what? What's the problem?
—SA
Hugo Dionicio Albornoz 28-Oct-14 18:10pm    
--- posted by mistake ---
Hugo Dionicio Albornoz 28-Oct-14 18:14pm    
I have 02 tables (horas_trabajadas and horas_paradas) need to get a dynamic table columns go vertical to horizontal with the sum of both tables by date range and shift.

Table: horas_trabajadas

equ_codigo || tur_codigo || con_fecha || hor_tra_horas
----------------------------------------------------------
01140803 || D || 2014-10-27 || 8.00
01140803 || N || 2014-10-27 || 7.00
01140803 || D || 2014-10-28 || 6.00
01140803 || N || 2014-10-28 || 5.00

Table: horas_paradas

equ_codigo || tur_codigo || con_fecha || hor_tra_horas
----------------------------------------------------------
01140803 || D || 2014-10-27 || 2.00
01140803 || N || 2014-10-27 || 3.00
01140803 || D || 2014-10-28 || 4.00
01140803 || N || 2014-10-28 || 5.00

EXPECTED RESULT:

Shift: Day
equ_codigo || 2014-10-27 || 2014-10-28
---------------------------------------------
01140803 || 10.00 || 10.00

Shift: night
equ_codigo || 2014-10-27 || 2014-10-28
---------------------------------------------
01140803 || 10.00 || 10.00
Hugo Dionicio Albornoz 28-Oct-14 18:14pm    
--- posted by mistake ---
Maciej Los 29-Oct-14 2:27am    
Please, use "Improve question" widget to update question. Do not post any code in comment!

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900