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