IF OBJECT_ID('TEMPDB..#TEMPTABLA') IS NOT NULL BEGIN DROP TABLE #TEMPTABLA;END
CREATE TABLE #TEMPTABLA
(COD INT,FEC DATE, HORENT TIME,HORSAL TIME,HORCOM TIME,TOTHOR TIME,SEC INT);
INSERT INTO #TEMPTABLA(COD,FEC, HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,horcal_fecini, horcal_ent_1,horcal_sal_1,'00:30',DATEDIFF(HOUR,horcal_ent_1,horcal_sal_1) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,1,horcal_fecini) ,horcal_ent_2,horcal_sal_2,'00:30',DATEDIFF(MINUTE,horcal_ent_2,horcal_sal_2) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,2,horcal_fecini),horcal_ent_3,horcal_sal_3,'00:30',DATEDIFF(MINUTE,horcal_ent_3,horcal_sal_3) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,3,horcal_fecini),horcal_ent_4,horcal_sal_4,'00:30',DATEDIFF(MINUTE,horcal_ent_4,horcal_sal_4) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,4,horcal_fecini),horcal_ent_5,horcal_sal_5,'00:30',DATEDIFF(MINUTE,horcal_ent_5,horcal_sal_5) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,5,horcal_fecini),horcal_ent_6,horcal_sal_6,'00:30',DATEDIFF(MINUTE,horcal_ent_6,horcal_sal_6) ,horcal_sec FROM TBL_HORCAL
INSERT INTO #TEMPTABLA(COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC)
SELECT horcal_cod,DATEADD(DAY,6,horcal_fecini),horcal_ent_7,horcal_sal_7,'00:30',DATEDIFF(MINUTE,horcal_ent_7,horcal_sal_7), horcal_sec FROM TBL_HORCAL
SELECT COD,FEC,HORENT,HORSAL,HORCOM,TOTHOR,SEC FROM #TEMPTABLA
WHERE HORENT IS NOT NULL
GROUP BY COD,FEC,HORENT,HORSAL,SEC,TOTHOR,HORCOM ORDER BY COD;
if Object_id('TEMPDB..#TEMPTABLA') is not null
begin
drop table #temptabla;
end
create table #temptabla
(
cod int,
fec date,
horent time,
horsal time,
horcom time,
tothor time,
sec int
);
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
horcal_fecini,
horcal_ent_1,
horcal_sal_1,
'00:30',
Datediff(hour, horcal_ent_1, horcal_sal_1),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 1, horcal_fecini),
horcal_ent_2,
horcal_sal_2,
'00:30',
Datediff(minute, horcal_ent_2, horcal_sal_2),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 2, horcal_fecini),
horcal_ent_3,
horcal_sal_3,
'00:30',
Datediff(minute, horcal_ent_3, horcal_sal_3),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 3, horcal_fecini),
horcal_ent_4,
horcal_sal_4,
'00:30',
Datediff(minute, horcal_ent_4, horcal_sal_4),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 4, horcal_fecini),
horcal_ent_5,
horcal_sal_5,
'00:30',
Datediff(minute, horcal_ent_5, horcal_sal_5),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 5, horcal_fecini),
horcal_ent_6,
horcal_sal_6,
'00:30',
Datediff(minute, horcal_ent_6, horcal_sal_6),
horcal_sec
from tbl_horcal
insert into #temptabla
(cod,
fec,
horent,
horsal,
horcom,
tothor,
sec)
select horcal_cod,
Dateadd(day, 6, horcal_fecini),
horcal_ent_7,
horcal_sal_7,
'00:30',
Datediff(minute, horcal_ent_7, horcal_sal_7),
horcal_sec
from tbl_horcal
select cod,
fec,
horent,
horsal,
horcom,
tothor,
sec
from #temptabla
where horent is not null
group by cod,
fec,
horent,
horsal,
sec,
tothor,
horcom
order by cod;