SELECT CAST('Meta' AS VARCHAR(15)) AS "Período",
0 AS "0",
15 AS "1",
30 AS "2",
45 AS "3",
60 AS "4",
75 AS "5",
90 AS "6",
105 AS "7",
120 AS "8",
135 AS "9",
150 AS "10",
165 AS "11",
180 AS "12",
195 AS "13"
UNION
SELECT CAST('1º trimestre' AS VARCHAR(15)) AS "Período",
0 AS "0",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) = 1 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 1 THEN 1 ELSE 0 END), 0) AS "1",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 2 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 2 THEN 1 ELSE 0 END), 0) AS "2",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 3 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 3 THEN 1 ELSE 0 END), 0) AS "3",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 4 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 4 THEN 1 ELSE 0 END), 0) AS "4",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 5 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 5 THEN 1 ELSE 0 END), 0) AS "5",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 6 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 6 THEN 1 ELSE 0 END), 0) AS "6",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 7 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 7 THEN 1 ELSE 0 END), 0) AS "7",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 8 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 8 THEN 1 ELSE 0 END), 0) AS "8",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 9 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 9 THEN 1 ELSE 0 END), 0) AS "9",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 10 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 10 THEN 1 ELSE 0 END), 0) AS "10",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 11 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 11 THEN 1 ELSE 0 END), 0) AS "11",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 12 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 12 THEN 1 ELSE 0 END), 0) AS "12",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 13 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 13 THEN 1 ELSE 0 END), 0) AS "13"
FROM esiaprocesso p
LEFT JOIN esiaCategoriaEvento c ON p.cdCategoriaevento = c.cdCategoriaEvento
WHERE date_part('year', p.dtInicio) = 2018
AND CAST(EXTRACT(QUARTER FROM P.dtInicio) AS INTEGER) = 1
AND c.cdCategoriaEvento = 2
AND p.cdTipoPendencia = 1
AND p.cdEntidade <> 526
AND p.cdProduto in (3,10,12,4,8,16,21)
UNION
SELECT CAST('2º trimestre' AS VARCHAR(15)) AS "Período",
0 AS "0",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) = 14 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 14 THEN 1 ELSE 0 END), 0) AS "1",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 15 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 15 THEN 1 ELSE 0 END), 0) AS "2",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 16 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 16 THEN 1 ELSE 0 END), 0) AS "3",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 17 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 17 THEN 1 ELSE 0 END), 0) AS "4",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 18 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 18 THEN 1 ELSE 0 END), 0) AS "5",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 19 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 19 THEN 1 ELSE 0 END), 0) AS "6",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 20 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 20 THEN 1 ELSE 0 END), 0) AS "7",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 21 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 21 THEN 1 ELSE 0 END), 0) AS "8",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 22 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 22 THEN 1 ELSE 0 END), 0) AS "9",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 23 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 23 THEN 1 ELSE 0 END), 0) AS "10",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 24 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 24 THEN 1 ELSE 0 END), 0) AS "11",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 25 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 25 THEN 1 ELSE 0 END), 0) AS "12",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 26 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 26 THEN 1 ELSE 0 END), 0) AS "13"
FROM esiaprocesso p
LEFT JOIN esiaCategoriaEvento c ON p.cdCategoriaevento = c.cdCategoriaEvento
WHERE date_part('year', p.dtInicio) = 2018
AND CAST(EXTRACT(QUARTER FROM P.dtInicio) AS INTEGER) = 2
AND c.cdCategoriaEvento = 2
AND p.cdTipoPendencia = 1
AND p.cdEntidade <> 526
AND p.cdProduto in (3,10,12,4,8,16,21)
UNION
SELECT CAST('3º trimestre' AS VARCHAR(15)) AS "Período",
0 AS "0",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) = 27 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 27 THEN 1 ELSE 0 END), 0) AS "1",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 28 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 28 THEN 1 ELSE 0 END), 0) AS "2",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 29 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 29 THEN 1 ELSE 0 END), 0) AS "3",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 30 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 30 THEN 1 ELSE 0 END), 0) AS "4",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 31 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 31 THEN 1 ELSE 0 END), 0) AS "5",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 32 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 32 THEN 1 ELSE 0 END), 0) AS "6",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 33 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 33 THEN 1 ELSE 0 END), 0) AS "7",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 34 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 34 THEN 1 ELSE 0 END), 0) AS "8",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 35 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 35 THEN 1 ELSE 0 END), 0) AS "9",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 36 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 36 THEN 1 ELSE 0 END), 0) AS "10",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 37 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 37 THEN 1 ELSE 0 END), 0) AS "11",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 38 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 38 THEN 1 ELSE 0 END), 0) AS "12",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 39 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 39 THEN 1 ELSE 0 END), 0) AS "13"
FROM esiaprocesso p
LEFT JOIN esiaCategoriaEvento c ON p.cdCategoriaevento = c.cdCategoriaEvento
WHERE date_part('year', p.dtInicio) = 2018
AND CAST(EXTRACT(QUARTER FROM P.dtInicio) AS INTEGER) = 3
AND c.cdCategoriaEvento = 2
AND p.cdTipoPendencia = 1
AND p.cdEntidade <> 526
AND p.cdProduto in (3,10,12,4,8,16,21)
UNION
SELECT CAST('4º trimestre' AS VARCHAR(15)) AS "Período",
0 AS "0",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) = 40 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 40 THEN 1 ELSE 0 END), 0) AS "1",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 41 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 41 THEN 1 ELSE 0 END), 0) AS "2",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 42 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 42 THEN 1 ELSE 0 END), 0) AS "3",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 43 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 43 THEN 1 ELSE 0 END), 0) AS "4",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 44 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 44 THEN 1 ELSE 0 END), 0) AS "5",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 45 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 45 THEN 1 ELSE 0 END), 0) AS "6",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 46 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 46 THEN 1 ELSE 0 END), 0) AS "7",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 47 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 47 THEN 1 ELSE 0 END), 0) AS "8",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 48 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 48 THEN 1 ELSE 0 END), 0) AS "9",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 49 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 49 THEN 1 ELSE 0 END), 0) AS "10",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 50 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 50 THEN 1 ELSE 0 END), 0) AS "11",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 51 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 51 THEN 1 ELSE 0 END), 0) AS "12",
NULLIF(SUM(CASE WHEN CAST(EXTRACT(WEEK FROM P.dtInicio) AS INTEGER) <= 52 AND CAST(EXTRACT(WEEK FROM CURRENT_DATE) AS INTEGER) >= 52 THEN 1 ELSE 0 END), 0) AS "13"
FROM esiaprocesso p
LEFT JOIN esiaCategoriaEvento c ON p.cdCategoriaevento = c.cdCategoriaEvento
WHERE date_part('year', p.dtInicio) = 2018
AND CAST(EXTRACT(QUARTER FROM P.dtInicio) AS INTEGER) = 4
AND c.cdCategoriaEvento = 2
AND p.cdTipoPendencia = 1
AND p.cdEntidade <> 526
AND p.cdProduto in (3,10,12,4,8,16,21)
ORDER BY 1 |