CALL GUS_reports('Z12.select.HR', 'gus_klasyf_zawodow Z ON P.ZawodWykonGUSkod = Z.kodGUS ', '2018', '10', '', @Z12.RepContent);
-- GUS Z-12

CALL GUS_reports('Z12.select.HR', 'zawody Z ON P.ZawodWykonGUS = Z.idn', '2018', '10', '', @Z12.RepContent);

SELECT 
P.DataZatrudnieniaWzakl, P.DataZwolnienia,
P.GUID, SUBSTR(Z.KodGUS, 1, 6) AS kod, O.plec, O.nazwisko, O.imiona 
FROM Pracownicy P 
JOIN osoby O ON P.guid = O.GUID 
 JOIN frekw F ON F.PracGUID = P.GUID 
 AND  (SELECT COUNT(*) AS ile  FROM frekw_po FP JOIN frekw FR ON FR.GUID=FP.FrekwGUID WHERE FP.absencja IN ('J','C','L','K','M','R','N','Z','A','Y')  
 AND YEAR(FP.rok_msc_dzien)= 2018 AND MONTH(FP.rok_msc_dzien)=10 AND FR.PracGUID=P.GUID  AND FR.deleted = 0 ) = 0 AND 
 (SELECT COUNT(*) AS ile  FROM frekw_po FP JOIN frekw FR ON FR.GUID=FP.FrekwGUID WHERE FP.absencja IN ('B') AND 
 YEAR(FP.rok_msc_dzien)= 2018 AND MONTH(FP.rok_msc_dzien)=10 AND FR.PracGUID=P.GUID  AND FR.deleted = 0 ) <= 10 AND F.rok = 2018 AND F.miesiac = 10  AND F.deleted = 0 
  JOIN prac_gus G ON G.guid_pracownika=P.guid AND (G.PracaNakladcza=0 AND G.ZatrPrzygotZawod = 0 AND G.Agent = 0 AND G.ZatrPraceInterw=0) 
 LEFT JOIN zawody Z ON P.ZawodWykonGUS = Z.idn
WHERE 
(P.ToPracownik = 1 OR P.ProwadziDzialalnosc = 1) AND  P.DataZatrudnieniaWzakl <= '2018-10-01' 
 -- and (ifNULL(P.DataZwolnienia, '2099-01-01') > '2018-10-31') 
GROUP BY P.GUID 
ORDER BY kod ASC, O.plec DESC, O.nazwisko, O.imiona ASC ;


SELECT FP.absencja FROM frekw_po