PHP array from MySQL result has duplicate values due to subquery -


i've used mysql query array of results php. however, query uses subquery , can result in subquery value being shown twice in array incorrect. spent ages looking @ sql see if stop wasn't successful @ all. wonder if easier modify array rectify error.

the array:

    array(436) {   [0]=>   array(8) {     ["id"]=>     string(2) "75"     ["subtotal"]=>     string(8) "168.0000"     ["invoice_type"]=>     string(1) "1"     ["adjustment"]=>     string(4) "0.00"     ["totalcost"]=>     string(8) "168.0000"   }   [1]=>   array(8) {     ["id"]=>     string(2) "82"     ["subtotal"]=>     string(7) "84.0000"     ["invoice_type"]=>     string(1) "1"     ["adjustment"]=>     string(4) "0.00"     ["totalcost"]=>     string(7) "84.0000"   }   [2]=>   array(8) {     ["id"]=>     string(2) "86"     ["subtotal"]=>     string(8) "224.0000"     ["invoice_type"]=>     string(1) "1"     ["adjustment"]=>     string(4) "0.00"     ["totalcost"]=>     string(8) "224.0000"   }   [3]=>   array(8) {     ["id"]=>     string(2) "95"     ["subtotal"]=>     string(7) "70.0000"     ["invoice_type"]=>     string(1) "1"     ["adjustment"]=>     string(4) "9.00"     ["totalcost"]=>     string(7) "70.0000"   }   [4]=>   array(8) {     ["id"]=>     string(2) "95"     ["subtotal"]=>     string(7) "84.0000"     ["invoice_type"]=>     string(1) "2"     ["adjustment"]=>     string(4) "9.00"     ["totalcost"]=>     string(7) "84.0000"   } 

the problem:

in sql, result grouped "invoice_type" "id". sql uses subquery value "adjustment". if result has "invoice_type = 1" , "invoice_type = 2", both items added array adjustment value added both don't want gets doubled.

is there simple way see if "id" duplicated , if is, set 1 of "adjustment" values 0.00?

if think should cover in sql, i'd happy add more information regarding part too.

i've been staring @ long , can't see other options ;-)

edit - sql:

select tbl_client.id, tbl_client.first_name, tbl_client.surname, tbl_schedule.invoice_type, sum( duration ) totalhours, sum( duration * rate ) subtotal, ifnull( adjustment.totaladjustment, 0 ) adjustment, ( sum( duration * rate ) + ifnull( adjustment.totaladjustment, 0 ) ) totalcost `tbl_schedule` inner join tbl_client on tbl_client.id = tbl_schedule.client left join (     select client, sum( amount ) totaladjustment     tbl_invoice_adjustment     ( tbl_invoice_adjustment.`date` between 1357002061 , 1359676740 )     group client     ) adjustment on adjustment.client = tbl_schedule.client tbl_schedule.franchise =1 , ( status =1 or `status` =2 or `status` =4 ) , `date` between 1357002061 , 1359676740 group tbl_schedule.invoice_type, tbl_schedule.client order tbl_client.surname asc 

edit - table structure

tbl_client

id
franchise
title
surname
first_name

tbl_schedule

id
franchise
date
client
invoice_type
duration
rate
status

tbl_invoice_adjustment

id
franchise
client
date
description
amount

the idea query should pull in adjustments given time period , combine them schedule values. till started implementing adjustment thing. 99.7% ok except rare case there multiple "invoice_type" entries given client in single period.

edit: - sql fiddle

here sql fiddle of problem.

if run statement, see client id 5 given adjustment twice though there 1 adjustment client in database. main query shows 2 rows because grouping invoice_type. each result row runs subquery , gets adjustment.

the fiddle shows required output statement not fixed in way.

i fixed using php method , running on array making change necessary. works needs.

 $showerror = false;     $lineid = array();     foreach ($empresults $invoiceline => $line) {         if (isset($lineid[$line['id']])) {             $empresults[$invoiceline]['duplicate'] = true;             $empresults[$lineid[$line['id']]]['duplicate'] = true;             if ($empresults[$lineid[$line['id']]]['adjustment'] != 0) {                 $empresults[$lineid[$line['id']]]['totalcost'] = $empresults[$lineid[$line['id']]]['totalcost'] - $empresults[$lineid[$line['id']]]['adjustment'];                 $empresults[$lineid[$line['id']]]['adjustment'] = 0;             }             $showerror = true;         } else {             $empresults[$invoiceline]['duplicate'] = false;         }         $lineid[$line['id']] = $invoiceline;     } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -