Sum Total Error

Sum Total Error

rafaswrafasw Posts: 75Questions: 7Answers: 0

I'm trying to add up the amount that each customer owes but it's adding up wrong, besides the sum being wrong, it only returns the sum for one customer

<?php

/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */

// DB table to use
$table = 'pedidos';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => '`p`.`id`', 'dt' => 0, 'field' => 'id' ),
    array( 'db' => '`u`.`nome`',  'dt' => 1, 'field' => 'nome' ),
    array( 'db' => '`e`.`empresa`',  'dt' => 2, 'field' => 'empresa' ),
    array( 'db' => 'v.valor',  'dt' => 3, 'field' => 'valor',)
);

// SQL server connection information
require('config.php');
$sql_details = array(
    'user' => $db_username,
    'pass' => $db_password,
    'db'   => $db_name,
    'host' => $db_host
);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

// require( 'ssp.class.php' );
require('ssp.customized.class.php' );

$joinQuery = "FROM `pedidos` AS `p` LEFT JOIN `usuarios` AS `u` ON (`u`.`id` = `p`.`id_cliente`) LEFT JOIN `empresas` AS `e` ON (`e`.`id` = `u`.`id_empresa`) LEFT JOIN `vendas` AS `v` ON ( v.id_pedido = p.id)  ";
//$extraWhere = "`u`.`salary` >= 90000";
//$groupBy = "`u`.`office`";
//$having = "`u`.`salary` >= 140000";

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraWhere, $groupBy, $having )
);


I already tried like this, but it only returns 1 result but it is supposed to return all customers with the sum

array( 'db' => 'sum(v.valor) as valor',  'dt' => 3, 'field' => 'valor',)

Answers

  • rafaswrafasw Posts: 75Questions: 7Answers: 0
    array( 'db' => 'v.valor',  'dt' => 3, 'field' => 'valor',)
    
    

    array( 'db' => 'sum(v.valor) as valor ',  'dt' => 3, 'field' => 'valor',)
    
    

    correct calculation = 1.756,00

    wrong calculation = 807684

    it is also not looking for the sum of other customers' bills

Sign In or Register to comment.