0
So, I need to make a SELECT that searches the bank for information about all the activities of the companies and returns the sum of all the "QNTD_PROCESSOS" of that activity. I’m trying to make a select Die along with the distinct, but am not succeeding.
I’ll show you how far I’ve come and then tell you how I’d like you to stay. I will post the jQuery code but it is not very necessary to understand the question.
function preencher_empresas() {
var conta; //Variável para controlar o número de empresas que vem do banco e para manipular o "loader" da página
$.ajax({
//dataType: "json",
type: "POST",
url: "../banco/banco-vision/pagina-relatorios/preencher-relatorio.php",
cache: false,
}).done(function(data) {
var relatorio = "";
$("#registros-relatorios").empty(); //LIMPAR LISTA
$.each($.parseJSON(data), function(chave, relat) {
//CRIANDO AS LINHAS COM OS TD DA TABELA QUE SÃO O RESULTADO NA CONSULTA AO BANCO
relatorio += '<tr>';
relatorio += '<td>' + relat.DEPARTAMENTO + '</td>';
relatorio += '<td>' + relat.COD + '</td>';
relatorio += '<td>' + relat.EMPRESAS + '</td>';
relatorio += '<td>' + relat.TRIBUTACAO + '</td>';
relatorio += '<td>' + relat.TIPO_ATIVIDADE + '</td>';
relatorio += '<td>' + relat.QNTD_PROCESSOS + '</td>';
relatorio += '</tr>';
});
$('#registros-relatorios').html(relatorio);
$("div#loading-relatorio").hide();
}).fail(function() {
alert('Falha na listagem dos usuários');
}).always(function() {});
}
<?php
//ARQUIVO CRIADO PARA PREENCHER A TABELA COM DADOS DO BANCO AO CARREGAR A PÁGINA
date_default_timezone_set('America/Sao_Paulo');
require_once("../../conexao/conexao-com-banco.php"); // CHAMANDO O ARQUIVO DE CONEXÃO AO BANCO
// CONSULTA GERAL DO BANCO QUE RETORNA O RESULTADO DA CONSULTA DA PÁGINA INTERFACE.PHP
session_start();
$departamento_usuario = $_SESSION["departamento-usuario"];
$select_relatorio = "SELECT DEPARTAMENTO, RESPONSAVEL, COD, EMPRESAS, TRIBUTACAO, TIPO_ATIVIDADE, QNTD_PROCESSOS FROM tbl_atividades WHERE DEPARTAMENTO = '$departamento_usuario' ORDER BY EMPRESAS";
$lista_relatorio = mysqli_query($conecta, $select_relatorio);
if(!$lista_relatorio)
{
die("Erro no Banco - Erro no select na tabela tbl_atividades");
exit;
}
$retorno_relatorio = array();
while($linha_relatorio = mysqli_fetch_object($lista_relatorio))
{
$retorno_relatorio[] = $linha_relatorio;
}
//JSON QUE VAI PARA O RETORNO DO AJAX COM DADOS DA CONSULTA DO SELECT
echo json_encode($retorno_relatorio);
?>
IMAGE OF THE DESCRIPTION OF THE TABLE IN WHICH THE QUERY IS MADE.
IMAGE OF HOW IT IS COMING OUT:
If you notice, some activities repeat themselves. I would like them not to be repeated and there in the TD "Executed Processes" shows the sum of all the activities that were equal (of the same type, of the same company). (I don’t know if I could be clear).
I tried to do it with a SELECT DISTINCT, but I was unsuccessful. I managed to make sure that the activities do not repeat themselves, but I could not see a way to show the sum of these activities.
If you can help me, I’d appreciate it.
Have you tried using the
GROUP BY
, but theDISTINCT
would have to work, maybe remove PHP and JS from the question and put the database structure and query, the question gets better– Leonardo Barros
@Leonardobarros , how would this query look? I will put the table structure in which I make the query. The bank was not I who made it. The table is kind of unorganised, but it’s functional.
– Gato de Schrödinger
I had expressed myself badly. In fact I would like to get the sum of the amount of Processes of the repeating activities.
– Gato de Schrödinger