Skip to content

Instantly share code, notes, and snippets.

@felipyamorim
Forked from antoniorc1972/painel.php
Last active December 7, 2018 20:18
Show Gist options
  • Select an option

  • Save felipyamorim/1f32e8c76c9aa99245f19a78880271b8 to your computer and use it in GitHub Desktop.

Select an option

Save felipyamorim/1f32e8c76c9aa99245f19a78880271b8 to your computer and use it in GitHub Desktop.
$sql=" SELECT distinct
cs.cd_pac,
duracao,
DATEDIFF(DAY, cs.dt_dispensa_sol, (select min(cs1.dt_dispensa_sol) from capa_solicitacao cs1, rl_capa_prep cp1
where cs1.cd_dis <> '972' AND cs1.tp_solic = 'F' AND cs1.profilax_sol = 'F' AND cs1.cd_dis = cp1.cd_dis AND cs1.num_sol = cp1.num_sol
AND cs.cd_pac = cs1.cd_pac and cp1.co_seq_retorno_prep is not null and cp1.co_paciente_prep = tpp.co_paciente_prep and
cs1.dt_dispensa_sol > cs.dt_dispensa_sol) ) as diferenca,
cd_uf,
rz_dis
FROM capa_solicitacao cs
INNER JOIN tb_paciente_prep tpp
ON tpp.cd_pac = cs.cd_pac
INNER JOIN rl_capa_prep cp
ON cp.co_paciente_prep = tpp.co_paciente_prep
INNER JOIN dispensador d
ON cs.cd_dis = d.cd_dis
INNER JOIN cidade c
ON d.cd_cid = c.cd_cid
INNER JOIN item_solicitacao_novo isn
ON isn.cd_dis = cs.cd_dis and isn.num_sol = cs.num_sol
WHERE co_pid IS NOT NULL
AND cs.cd_dis <> '972'
AND cs.cd_dis in ('742', '617', '001', '238', '058', '036', '057', '780', '101', '157', '088', '083','070', '005','284')
AND cs.tp_solic = 'F'
AND cs.profilax_sol = 'F'
AND cs.cd_dis = cp.cd_dis
AND cs.num_sol = cp.num_sol
AND cp.co_seq_atendimento_prep IS NOT NULL
AND cs.dt_dispensa_sol IN (SELECT MIN(cs2.dt_dispensa_sol)
FROM capa_solicitacao cs2,
rl_capa_prep cp2
WHERE cs2.cd_dis = cp2.cd_dis
AND cs2.num_sol = cp2.num_sol
AND cs2.tp_solic = 'F'
AND cs2.profilax_sol = 'F'
AND cp2.co_seq_atendimento_prep
IS NOT NULL
AND cp2.co_paciente_prep = tpp.co_paciente_prep)
AND EXISTS (
SELECT *
FROM rl_capa_prep cp2
WHERE cp2.co_seq_retorno_prep IS NOT NULL
AND cp2.co_paciente_prep = tpp.co_paciente_prep
) and
isn.duracao in (select MIN(duracao) from item_solicitacao_novo isn1 where isn1.cd_dis= isn.cd_dis and isn1.num_sol = isn.num_sol)
order by cd_uf,rz_dis, duracao ";
$stmt = $pdo->query($sql);
$result3_imprep = $stmt->fetchAll();
$total3_imprep = count($result3_imprep);
$result3_imprep = array_map(function($item){
$media = $item['diferenca'] ? $item['duracao'] / $item['diferenca'] : 1.1;
return [
'cd_pac' => $item['cd_pac'],
'cd_uf' => $cd_uf,
'rz_dis' => $rz_dis,
'media' => $media
];
}, $result3_imprep);
$result3_imprep = array_reduce($result3_imprep, function($carry, $item){
$key = md5($item['rz_dis'] . ' - ' . $item['rz_dis']);
if(empty($carry[$key])){
$carry[$key] = [
'cd_uf' => $item['cd_uf'],
'rz_dis' => $item['rz_dis'],
'aderido' => 0,
'nao_aderido' 0
];
}
if($item['media'] >= 1.07){
$carry[$key]['aderido']++;
}
if($item['media'] < 1.07){
$carry[$key]['nao_aderido']++;
}
return $carry;
});
echo '<pre>';
var_dump($result3_imprep);
die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment