Skip to content

Commit 86a2183

Browse files
authored
Merge pull request #592 from ezanon/master
Contagem pós-docs #591
2 parents e7148e2 + 1be75f6 commit 86a2183

3 files changed

Lines changed: 163 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
SELECT
2+
anos.Ano,
3+
COUNT(p.codprj) AS qtdProjetosAtivos
4+
FROM (
5+
SELECT DISTINCT YEAR(dtainiprj) AS Ano
6+
FROM PDPROJETO
7+
WHERE codund IN (__codundclg__)
8+
AND staatlprj IN (__statuses__)
9+
AND dtainiprj IS NOT NULL
10+
AND YEAR(dtainiprj) <= YEAR(GETDATE())
11+
UNION
12+
SELECT DISTINCT YEAR(dtafimprj) AS Ano
13+
FROM PDPROJETO
14+
WHERE codund IN (__codundclg__)
15+
AND staatlprj IN (__statuses__)
16+
AND dtafimprj IS NOT NULL
17+
AND YEAR(dtafimprj) <= YEAR(GETDATE())
18+
) anos
19+
LEFT JOIN PDPROJETO p
20+
ON p.codund IN (__codundclg__)
21+
AND p.staatlprj IN (__statuses__)
22+
AND p.dtainiprj IS NOT NULL
23+
AND YEAR(p.dtainiprj) <= anos.Ano
24+
AND (
25+
p.dtafimprj IS NULL
26+
OR YEAR(p.dtafimprj) >= anos.Ano
27+
)
28+
GROUP BY anos.Ano
29+
ORDER BY anos.Ano;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
SELECT
2+
/* Formato YYYY-MM sem usar estilo 120 */
3+
CAST(YEAR(dt) AS VARCHAR(4))
4+
+ '-' +
5+
RIGHT('0' + CAST(MONTH(dt) AS VARCHAR(2)), 2) AS AnoMes,
6+
DATENAME(month, dt) AS NomeMes,
7+
YEAR(dt) AS Ano,
8+
COUNT(p.codprj) AS qtdProjetosAtivos
9+
FROM (
10+
/* Geração manual dos últimos 12 meses (0 a 11) */
11+
SELECT DATEADD(
12+
month,
13+
-Nums.Num,
14+
DATEADD(day, 1 - DAY(GETDATE()), GETDATE())
15+
) AS dt,
16+
Nums.Num
17+
FROM (
18+
SELECT 0 AS Num
19+
UNION ALL SELECT 1
20+
UNION ALL SELECT 2
21+
UNION ALL SELECT 3
22+
UNION ALL SELECT 4
23+
UNION ALL SELECT 5
24+
UNION ALL SELECT 6
25+
UNION ALL SELECT 7
26+
UNION ALL SELECT 8
27+
UNION ALL SELECT 9
28+
UNION ALL SELECT 10
29+
UNION ALL SELECT 11
30+
) Nums
31+
) Meses
32+
LEFT JOIN PDPROJETO p
33+
ON p.codund IN (__codundclg__)
34+
AND p.staatlprj IN (__statuses__)
35+
AND p.dtainiprj <= DATEADD(day, -1, DATEADD(month, 1, dt))
36+
AND (p.dtafimprj IS NULL OR p.dtafimprj >= dt)
37+
GROUP BY
38+
CAST(YEAR(dt) AS VARCHAR(4))
39+
+ '-' +
40+
RIGHT('0' + CAST(MONTH(dt) AS VARCHAR(2)), 2),
41+
DATENAME(month, dt),
42+
YEAR(dt),
43+
Meses.Num,
44+
dt
45+
ORDER BY
46+
YEAR(dt),
47+
MONTH(dt);

src/Pesquisa.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,92 @@ public static function listarPesquisaPosDoutorandos()
122122

123123
return $pesquisas_pos_doutorando;
124124
}
125+
126+
/**
127+
* Retorna a quantidade de projetos PD por ano.
128+
*
129+
* Quando o parâmetro $statuses é nulo ou vazio, a função identifica
130+
* automaticamente todos os valores distintos existentes na coluna
131+
* `staatlprj` da tabela PDPROJETO, utilizando-os como filtro.
132+
*
133+
* Caso contrário, usa exatamente os status informados ou o padrão.
134+
*
135+
* Retorno:
136+
* - Array associativo onde:
137+
* - chave = ano (int)
138+
* - valor = quantidade de projetos nesse ano (int)
139+
*
140+
* @author Erickson Zanon
141+
* @date 14/11/2025
142+
* @param array|null $statuses Lista de status a filtrar ou NULL para autodetecção
143+
* @return array
144+
*/
145+
146+
public static function contarPDporAno($statuses = ['Ativo','Aprovado'])
147+
{
148+
if (($statuses === null) or (count($statuses)==0)) {
149+
$query = "SELECT DISTINCT staatlprj FROM PDPROJETO";
150+
$result = DB::fetchAll($query);
151+
$statuses = [];
152+
foreach ($result as $row) {
153+
if (!empty($row['staatlprj'])) {
154+
$statuses[] = $row['staatlprj'];
155+
}
156+
}
157+
}
158+
$query = DB::getQuery('Pesquisa.contarPDporAno.sql');
159+
$statusesString = "'" . implode("','", $statuses) . "'";
160+
$query = str_replace('__statuses__', $statusesString, $query);
161+
$contagem = DB::fetchAll($query);
162+
$contagemPorAno = array();
163+
foreach ($contagem as $c){
164+
$contagemPorAno[$c['Ano']] = $c['qtdProjetosAtivos'];
165+
166+
}
167+
return $contagemPorAno;
168+
}
169+
170+
/**
171+
* Retorna a quantidade de projetos PD por mês nos últimos 12 meses.
172+
*
173+
* Quando o parâmetro $statuses é nulo ou vazio, a função identifica
174+
* automaticamente todos os valores distintos existentes na coluna
175+
* `staatlprj` da tabela PDPROJETO, utilizando-os como filtro.
176+
*
177+
* Caso contrário, usa exatamente os status informados ou o padrão.
178+
*
179+
* Retorno:
180+
* - Array associativo onde:
181+
* - chave = "YYYY-MM" (string)
182+
* - valor = quantidade de projetos nesse mês (int)
183+
*
184+
* @author Erickson Zanon
185+
* @date 14/11/2025
186+
* @param array|null $statuses Lista de status a filtrar ou NULL para autodetecção
187+
* @return array
188+
*/
189+
190+
public static function contarPDporUltimos12Meses($statuses = ['Ativo','Aprovado'])
191+
{
192+
if (($statuses === null) or (count($statuses)==0)) {
193+
$query = "SELECT DISTINCT staatlprj FROM PDPROJETO";
194+
$result = DB::fetchAll($query);
195+
$statuses = [];
196+
foreach ($result as $row) {
197+
if (!empty($row['staatlprj'])) {
198+
$statuses[] = $row['staatlprj'];
199+
}
200+
}
201+
}
202+
$query = DB::getQuery('Pesquisa.contarPDporUltimos12Meses.sql');
203+
$statusesString = "'" . implode("','", $statuses) . "'";
204+
$query = str_replace('__statuses__', $statusesString, $query);
205+
$contagem = DB::fetchAll($query);
206+
$contagemPorMes = array();
207+
foreach ($contagem as $c){
208+
$contagemPorMes[$c['AnoMes']] = $c['qtdProjetosAtivos'];
209+
}
210+
return $contagemPorMes;
211+
}
125212

126213
}

0 commit comments

Comments
 (0)