Advertisement
marcocunhasilva

Untitled

Feb 20th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.84 KB | None | 0 0
  1. SELECT P.PON_FUN, FUN_NOME,
  2.         CASE EXTRACT(WEEKDAY FROM CAST(PON_ENTRADA AS DATE))
  3.           WHEN 0 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Dom'
  4.           WHEN 1 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Seg'
  5.           WHEN 2 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Ter'
  6.           WHEN 3 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Qua'
  7.           WHEN 4 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Qui'
  8.           WHEN 5 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Sex'
  9.           WHEN 6 THEN LPAD(EXTRACT(DAY FROM PON_ENTRADA), 2, '0') || '/' || EXTRACT(MONTH FROM PON_ENTRADA) || ' - Sáb'
  10.         END DATA,
  11.         MARCACOES,
  12.         DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') TOTAL_HORAS,
  13.         CAST(EXP_SEG_SEX AS TIME) EXPEDIENTE,
  14.         IIF(DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') > CAST(EXP_SEG_SEX AS TIME),
  15.             DATEADD(SECOND, DATEDIFF(SECOND FROM CAST(EXP_SEG_SEX AS TIME) TO DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00')), TIME '00:00:00'),
  16.             CAST('00:00:00' AS TIME)) SALDO_POSITIVO,
  17.         IIF(DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') > CAST(EXP_SEG_SEX AS TIME),
  18.             CAST('00:00:00' AS TIME),
  19.             DATEADD(SECOND, DATEDIFF(SECOND FROM DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') TO CAST(EXP_SEG_SEX AS TIME)),TIME '00:00:00')) SALDO_NEGATIVO,
  20.         IIF(DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') > CAST(EXP_SEG_SEX AS TIME),
  21.             '+ ' || SUBSTRING(DATEADD(SECOND, DATEDIFF(SECOND FROM CAST(EXP_SEG_SEX AS TIME) TO DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00')), TIME '00:00:00') FROM 1 FOR 8),
  22.             '- ' || SUBSTRING(DATEADD(SECOND, DATEDIFF(SECOND FROM DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') TO CAST(EXP_SEG_SEX AS TIME)),TIME '00:00:00') FROM 1 FOR 8)) SALDO_IMPRESSAO,
  23.         IIF(DATEDIFF(SECOND FROM CAST(EXP_SEG_SEX AS TIME) TO DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00')) > 0,
  24.             DATEDIFF(SECOND FROM CAST(EXP_SEG_SEX AS TIME) TO DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00')),
  25.             0) TOTAL_HORA_EXTRA,
  26.         IIF(DATEDIFF(SECOND FROM CAST(EXP_SEG_SEX AS TIME) TO DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00')) < 0,
  27.             DATEDIFF(SECOND FROM DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') TO CAST(EXP_SEG_SEX AS TIME)),
  28.             0) TOTAL_ATRASO,
  29.         CAST(LIST(PON_OBS) AS VARCHAR(100)) OBS,
  30.         BANCO_HORAS_UTILIZADO,
  31.         EXTRACT(YEAR FROM PON_ENTRADA) ANO, EXTRACT(MONTH FROM PON_ENTRADA) MES
  32.  FROM (
  33.    SELECT P.*, FUN_NOME,
  34.            COALESCE(CASE EXTRACT(WEEKDAY FROM CAST(PON_ENTRADA AS DATE))
  35.              WHEN 0 THEN DOM.EXP_TOTAL_HORAS
  36.              WHEN 1 THEN SEG.EXP_TOTAL_HORAS
  37.              WHEN 2 THEN TER.EXP_TOTAL_HORAS
  38.              WHEN 3 THEN QUA.EXP_TOTAL_HORAS
  39.              WHEN 4 THEN QUI.EXP_TOTAL_HORAS
  40.              WHEN 5 THEN SEX.EXP_TOTAL_HORAS
  41.              WHEN 6 THEN SAB.EXP_TOTAL_HORAS
  42.            END, '00:00:00') EXP_SEG_SEX
  43.    FROM PONTO_FUNCIONARIO P
  44.    INNER JOIN FUNCIONARIOS ON ID_FUNCIONARIO = PON_FUN
  45.    LEFT JOIN EXPEDIENTES_PONTO SEG ON SEG.ID_EXPEDIENTE_PONTO = FUN_EXP_SEG
  46.    LEFT JOIN EXPEDIENTES_PONTO TER ON TER.ID_EXPEDIENTE_PONTO = FUN_EXP_TER
  47.    LEFT JOIN EXPEDIENTES_PONTO QUA ON QUA.ID_EXPEDIENTE_PONTO = FUN_EXP_QUA
  48.    LEFT JOIN EXPEDIENTES_PONTO QUI ON QUI.ID_EXPEDIENTE_PONTO = FUN_EXP_QUI
  49.    LEFT JOIN EXPEDIENTES_PONTO SEX ON SEX.ID_EXPEDIENTE_PONTO = FUN_EXP_SEX
  50.    LEFT JOIN EXPEDIENTES_PONTO SAB ON SAB.ID_EXPEDIENTE_PONTO = FUN_EXP_SAB
  51.    LEFT JOIN EXPEDIENTES_PONTO DOM ON DOM.ID_EXPEDIENTE_PONTO = FUN_EXP_DOM
  52.    WHERE CAST(PON_ENTRADA AS DATE) >= '01.10.2022' AND CAST(PON_SAIDA AS DATE) <= '31.10.2022'
  53.      AND PON_BANCO_HORAS IS NULL
  54.      AND PON_FUN = 584
  55.    ORDER BY PON_ENTRADA
  56.    ) P
  57.  INNER JOIN (
  58.    SELECT
  59.      DISTINCT PON_FUN,
  60.      CAST(PON_ENTRADA AS DATE) DATA,
  61.      (SELECT
  62.         RESULTADO
  63.       FROM FBLIST('SELECT COALESCE(SUBSTRING(CAST(PON_ENTRADA AS TIME) FROM 1 FOR 8), ''FALTA'') || '' - '' ||
  64.                          COALESCE(SUBSTRING(CAST(PON_SAIDA AS TIME) FROM 1 FOR 8), ''FALTA'')
  65.                   FROM PONTO_FUNCIONARIO
  66.                   WHERE PON_FUN = ' || F.PON_FUN || '
  67.                   AND CAST(PON_ENTRADA AS DATE) = ''' || CAST(F.PON_ENTRADA AS DATE) || '''
  68.                   ORDER BY PON_ENTRADA',
  69.                   ' - ')) MARCACOES
  70.    FROM PONTO_FUNCIONARIO F
  71.    WHERE CAST(PON_ENTRADA AS DATE) >= '01.10.2022' AND CAST(PON_SAIDA AS DATE) <= '31.10.2022'
  72.      AND PON_BANCO_HORAS IS NULL AND PON_FUN = 584
  73.  ) X ON X.PON_FUN = P.PON_FUN AND X.DATA = CAST(P.PON_ENTRADA AS DATE)
  74.  LEFT JOIN (
  75.    SELECT PON_FUN FUNCIONARIO, DATEADD(SECOND, SUM(DATEDIFF(SECOND, TIME '00:00:00', PON_TOTAL)), TIME '00:00:00') BANCO_HORAS_UTILIZADO
  76.    FROM PONTO_FUNCIONARIO
  77.    WHERE CAST(PON_ENTRADA AS DATE) >= '01.10.2022' AND CAST(PON_SAIDA AS DATE) <= '31.10.2022'
  78.      AND PON_BANCO_HORAS = 'S'
  79.      AND PON_FUN = 584
  80.      GROUP BY PON_FUN) ON FUNCIONARIO = P.PON_FUN
  81.  GROUP BY PON_FUN, FUN_NOME, DATA, MARCACOES, EXP_SEG_SEX,
  82.           BANCO_HORAS_UTILIZADO, ANO, MES
  83.  ORDER BY PON_FUN, ANO, MES, DATA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement