Last active
April 22, 2019 17:56
-
-
Save juniorUsca/88ed5274e235cd3b9062e0962745adb0 to your computer and use it in GitHub Desktop.
query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE [RRHH] | |
DECLARE @tnEmpresa TINYINT = 1; | |
DECLARE @tnOrganizacion SMALLINT = 16; | |
DECLARE @tnNivelTrabajador TINYINT = 1; | |
DECLARE @tnNumero INT = 1; | |
DECLARE @ltbAsistencia TABLE | |
( | |
nEmpleado INT, | |
nTotalDias SMALLINT | |
); | |
/* | |
-- --- obtenemos la asistencia del personal | |
INSERT INTO @ltbAsistencia | |
SELECT | |
A.nEmpleado, | |
CASE | |
WHEN T.nTotalDias IS NOT NULL AND T.nTotalDias != 0 THEN T.nTotalDias | |
ELSE M.nTotalDias | |
END | |
FROM | |
(--- obtenemos los empleados sin repetir... | |
SELECT | |
A.dDesde, A.dHasta, B.nEmpleado, B.cEmpleado | |
FROM Bonos.Bono A | |
CROSS APPLY | |
( | |
SELECT | |
BA.nEmpleado, '0' + CAST(BA.nEmpleado AS varchar(10)) cEmpleado | |
FROM Bonos.BonoDetalle BA | |
WHERE BA.nEmpresa = A.nEmpresa AND BA.nOrganizacion = A.nOrganizacion | |
AND BA.nNivelTrabajador = A.nNivelTrabajador AND BA.nNumero = A.nNumero | |
GROUP BY BA.nEmpleado | |
)B | |
WHERE A.nEmpresa = @tnEmpresa AND A.nOrganizacion = @tnOrganizacion | |
AND A.nNivelTrabajador = @tnNivelTrabajador AND A.nNumero = @tnNumero | |
)A | |
OUTER APPLY | |
( | |
SELECT | |
COUNT(1) nTotalDias, 8 nTotalHoras | |
FROM | |
( | |
SELECT | |
fjornal | |
FROM PlaniRP.dbo.Marcas MA | |
WHERE MA.cpersonal = A.cEmpleado AND MA.tipo = 1 | |
AND MA.fjornal >= A.dDesde AND MA.fjornal <= A.dHasta | |
GROUP BY fjornal | |
)MA | |
)M | |
OUTER APPLY | |
( | |
SELECT | |
--- solo contamos los dias con mayor a 8 horas | |
COUNT(CASE WHEN nHoras < 8 THEN NULL ELSE 1 END) nTotalDias, SUM(nHoras) nTotalHoras | |
FROM | |
( | |
SELECT | |
periodo, semana, lun_horas, mar_horas, mie_horas, jue_horas, vie_horas, sab_horas, dom_horas | |
FROM PlaniRp.dbo.Pl_TareoDet TA WITH (INDEX([PK_Pl_TareoDet])) | |
WHERE TA.Empresa = @tnEmpresa AND periodo = YEAR(A.dDesde) AND semana >= DATEPART(WK, A.dDesde) | |
AND Semana <= DATEPART(Wk, A.dHasta) | |
AND A.nEmpleado = TA.personal | |
) P | |
UNPIVOT(nHoras FOR cDia IN (lun_horas, mar_horas, mie_horas, jue_horas, vie_horas, sab_horas, dom_horas )) AS Unpvt | |
)T; | |
--*/ | |
INSERT INTO @ltbAsistencia | |
SELECT | |
ISNULL(SM.nEmpleado,ST.nEmpleado) as nEmpleado, | |
CASE | |
WHEN ST.nTotalDias IS NOT NULL AND ST.nTotalDias != 0 THEN ST.nTotalDias | |
ELSE ISNULL(SM.nTotalDias,0) | |
END as nTotalDias | |
FROM | |
( | |
select A.nEmpleado, COUNT(*) as nTotalDias | |
FROM ( | |
SELECT DISTINCT A.nEmpleado, MA.fjornal | |
from | |
( | |
SELECT DISTINCT B.nEmpleado, A.dDesde, A.dHasta, A.nOrganizacion, A.nNivelTrabajador, A.nNumero, A.nEmpresa | |
FROM Bonos.Bono A | |
join Bonos.BonoDetalle B | |
ON A.nEmpresa=B.nEmpresa | |
AND A.nOrganizacion=B.nOrganizacion | |
AND A.nNivelTrabajador=B.nNivelTrabajador | |
AND A.nNumero=B.nNumero | |
) A | |
JOIN PlaniRP.dbo.Marcas MA | |
ON MA.cpersonal = '0'+CAST(A.nEmpleado AS varchar(10)) | |
AND MA.tipo = 1 | |
AND MA.fjornal >= A.dDesde | |
AND MA.fjornal <= A.dHasta | |
WHERE A.nEmpresa = @tnEmpresa AND A.nOrganizacion = @tnOrganizacion | |
AND A.nNivelTrabajador = @tnNivelTrabajador AND A.nNumero = @tnNumero | |
) A | |
group by A.nEmpleado | |
) SM --suma de marcas | |
RIGHT JOIN | |
( | |
select A.nEmpleado, | |
SUM( | |
CASE WHEN ISNULL(lun_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(mar_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(mie_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(jue_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(vie_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(sab_horas,0) < 8 THEN 0 ELSE 1 END + | |
CASE WHEN ISNULL(dom_horas,0) < 8 THEN 0 ELSE 1 END) AS nTotalDias, | |
SUM(ISNULL(lun_horas,0) + | |
ISNULL(mar_horas,0) + | |
ISNULL(mie_horas,0) + | |
ISNULL(jue_horas,0) + | |
ISNULL(vie_horas,0) + | |
ISNULL(sab_horas,0) + | |
ISNULL(dom_horas,0)) AS nTotalHoras | |
from | |
( | |
SELECT DISTINCT B.nEmpleado, A.dDesde, A.dHasta, A.nOrganizacion, A.nNivelTrabajador, A.nNumero, A.nEmpresa | |
FROM Bonos.Bono A | |
join Bonos.BonoDetalle B | |
ON A.nEmpresa=B.nEmpresa | |
AND A.nOrganizacion=B.nOrganizacion | |
AND A.nNivelTrabajador=B.nNivelTrabajador | |
AND A.nNumero=B.nNumero | |
) A | |
LEFT JOIN PlaniRp.dbo.Pl_TareoDet TA | |
ON TA.Empresa = A.nEmpresa | |
AND TA.periodo = YEAR(A.dDesde) | |
AND semana >= DATEPART(WK, A.dDesde) | |
AND Semana <= DATEPART(Wk, A.dHasta) | |
AND A.nEmpleado = TA.personal | |
WHERE A.nEmpresa = @tnEmpresa AND A.nOrganizacion = @tnOrganizacion | |
AND A.nNivelTrabajador = @tnNivelTrabajador AND A.nNumero = @tnNumero | |
group by A.nEmpleado | |
) AS ST --suma tareo | |
ON SM.nEmpleado=ST.nEmpleado | |
-- SELECT -- | |
SELECT | |
A.nEmpresa | |
, A.nOrganizacion | |
, A.nNivelTrabajador | |
, A.nNumero | |
, A.nPeriodicidad | |
, B.cNombre as cPeriodicidad | |
, A.dDesde | |
, A.dHasta | |
, A.cObservacion | |
, A.lHereda | |
, A.cCondicion | |
, A.cEstado | |
, A.cUsuarioRegistro | |
, ISNULL(A.cUsuarioVerifica, '') cUsuarioVerifica | |
, ISNULL(A.cPeriodo,'') AS cPeriodo | |
, ISNULL(A.cSemana,'') AS cSemana | |
FROM Bonos.Bono A | |
INNER JOIN dbo.Periodicidad B ON B.nCodigo = A.nPeriodicidad | |
WHERE nEmpresa = @tnEmpresa AND nOrganizacion = @tnOrganizacion | |
AND nNivelTrabajador = @tnNivelTrabajador AND nNumero = @tnNumero; | |
-- DETALLE | |
SELECT | |
B.nPlantilla | |
, B.nEmpleado | |
, E.cApePaterno + ' ' + E.cApeMaterno + ', ' +E.cNombres AS cPersona | |
, G.cNombre AS cCargo | |
, B.nOrgEmpleado | |
, B.nTipoBono | |
, C.cAbreviatura + '_' + D.cTime AS cTipoBono | |
, B.nPuesto | |
, B.nFactor | |
, B.nTotalPEN | |
, B.nTotalUSD | |
, ISNULL(B.cNota, '') cNota | |
, ISNULL(B.cObservacion, '') cObservacion | |
, B.cEstado | |
,C.nPersonalLimitePEN | |
,ISNULL(H.nTotalDias, -1) nTotalDias | |
FROM Bonos.BonoDetalle B | |
INNER JOIN Bonos.BonoPlantilla C ON C.nOrganizacion = B.nOrganizacion AND C.nNivelTrabajador = B.nNivelTrabajador | |
AND C.nPlantilla = B.nPlantilla AND C.nTipoBono = B.nTipoBono | |
INNER JOIN Bonos.BonoTipo AS D ON D.nCodigo = C.nTipoBono | |
INNER JOIN dbo.Persona AS E ON E.nPersona = B.nEmpleado | |
INNER JOIN Planilla.PersonaOrganizacion AS F ON F.nPersona = B.nEmpleado AND F.nOrganizacion = B.nOrgEmpleado | |
INNER JOIN Planilla.PuestoOrganizacion AS G ON G.nEmpresa = F.nEmpresa AND G.nPuesto = F.nPuesto AND G.nOrganizacion = F.nOrganizacion | |
LEFT JOIN @ltbAsistencia H ON B.nEmpleado = H.nEmpleado | |
WHERE B.nEmpresa = @tnEmpresa AND B.nOrganizacion = @tnOrganizacion | |
AND B.nNivelTrabajador = @tnNivelTrabajador AND B.nNumero = @tnNumero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment