2 queries and 1 array

I have two queries I am running.  One is to get the listing of customers the other is to get those customes invoices.
I'm going to start off with a bit of a disclaimer.  I cannot do a table join on these two tables, they dont existing in the same location, they are on two seperate machines in two seperate RDMS.

1. list of customers and their customer numbers.
2. list of the invoices.
3. Customers whos Invoice total is 1500 and below are to be shown, those above 1500 are not to be shown.
4. Invoice totals of 0, blank, null, no value, empty are to be 0.00.


What I have so far

$db = odbc_connect("dns1", "u", "p") or die("could not connect");
$db2 = odbc_connect("dns2", "u", "p") or die("could not connect");
$result = odbc_exec($db, $query1);
if (odbc_error()) {
      echo odbc_errormsg($db);
}

$cunum=array();
$inovice=array();

while(odbc_fetch_row ( $result )){
      $customernumber=odbc_result($result,1);
      array_push($cunum, odbc_result($result,1), odbc_result($result,2));
      $result = odbc_exec($db, $query2);
      array_push($invoice, odbc_result($result2,1), odbc_result($result2,2), odbc_result($result2,3));
}

$grouped = array();


What I forgot to ask was is the correct, can this be done better more efficent, besides a table join.
iceman19330Asked:
Who is Participating?
 
ThGConnect With a Mentor Commented:
you have a one-to-many relationship betweens your data..so the approach would be to first collect the customers this way:

$customers = array();

while (...) {
  $customers[$record['customer_id']] = $record;
  $customers[$record['customer_id']]['invoice'] = array(); // will be useful for next step
  $customers[$record['customer_id']]['invoice_sum'] = 0.00; // will be useful for next step
}

as 'customer_id' must be unique, this will build up the array of your customers, indexed. now you can collect invoinces thinking as customers records as "buckets":

while (...) { // invoices
  $cust_id = $invoice_record['customer_id']; // foreign key
  array_push($customers[$cust_id]['invoices'], $invoice_record);
  // calculate meanwhile the invoices sum
  $customers[$cust_id]['invoices_sum'] += (float) $invoice_record['total'];
}

now loop your $customers array and display those that apply

foreach ($customers as $record) {
  if ($record['invoices_sum'] >= 1500.00)
    //display......
}
0
 
Richard QuadlingSenior Software DeveloperCommented:
What is the DB? I use MS SQL. I can talk to many SERVERs and many DBs in a SINGLE sql statement. No problem!

And I use ODBC for my connection to MS SQL.
0
 
iceman19330Author Commented:
DB2 to MySQL and vice versa.  The DB is in Canada via VPN and the MySQL is local.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
iceman19330Author Commented:
So the $customers[$record['customer_id']] = $record; part should be   $customers[$record[<FIELDNAME>]] = $record; that points to the customer number and  $cust_id = $invoice_record[<FIELDNAME>]; // foreign key should be for the field that points to the customer number in the invoice table.  Is that correct.

my next question is $customers[$cust_id]['invoices_sum'] += (float) $invoice_record['total']; supposed to be totalling up the individual invoices for that customer, if so the querey that I wrote already does that.
0
 
cristi_2112Connect With a Mentor Commented:
This is too much time consuming if you ask me.
If you have 'n' customers, you are doing (n + n) queries, when in fact you can do this in (n+1) queries.
If you have the array $customer_ids, you can do something like this:

$db = odbc_connect("dns1", "u", "p") or die("could not connect");
$db2 = odbc_connect("dns2", "u", "p") or die("could not connect");
$result = odbc_exec($db, $query1);
if (odbc_error()) {
     echo odbc_errormsg($db);
}

$customer_numbers = array();
$cunum=array();
$inovice=array();

while(odbc_fetch_row ( $result )){
     $customer_numbers[]=odbc_result($result,1);
     array_push($cunum, odbc_result($result,1), odbc_result($result,2));
}

$query = 'SELECT customer_id, SUM(invoice) AS invoice_sum FROM invoice_table WHERE customer_id IN (' . implode($customer_numbers, ',') . ') GROUP BY customer_id HAVING SUM(invoice) >= 1500';
$result2 = odbc_exec($db2, $query);
...
// link the two array-results from queries, by customer_id
...

this is much more faster for large number of customers
0
 
ThGCommented:
hell yeah. if you already grouped by customer id you can just update your local record:

while (...) { // invoices
  $cust_id = $invoice_record['customer_id']; // foreign key - grouping field!
  $customers[$cust_id]['invoices_sum'] = $invoice_record['total']
}

well in both my algorithms i used name-based record indexing, but as you are using odbc you probably need to use positional indexing.. you can easily sort out how to replace that
0
 
ThGCommented:
@cristi_2112:

where do you see (n + n) queries? the SQL queries are actually two in both cases, and the complexity is ~(n+m) in my first example and ~ (2n) in the second one, suggested by you, given that n is customers number and m total invoices number.
0
 
cristi_2112Commented:
I'm sorry, I must have been thinking of something else.
In my solution you have (1+1) = 2 queries. I select all the customers in one query, then I select the sums of invoices for all customers in one query. So with 2 queries, I get an array with the customers and another array with the sum of invoices, with customer_id as the key.
In your solution I don't see where you are running the query for selecting invoices, so maybe I assumed wrong.
If you do one query to select all the invoices in your $invoice_record array, then you are still losign some time to make the sum. Also I was thinking that maybe you don't need all the invoices from the second table - maybe there are some invoices of customers that you don't have in the first table.
But I am not arguing with anybody. I just told you my best way of handling this. If I didn't understand exaclty what you where saying, hell too bad.
0
 
ThGCommented:
i'm not arguing either :-) i was just being picky. in fact your method (grouping the invoices) *IS* better. BUT from the point of view of SQL queries, they both take 1+1 queries, thru odbc.

in my first example i was selecting ALL invoices in one shot, not a single select for each customer.

regards
0
 
iceman19330Author Commented:
Thank you guys for the discussion and different view points while I used ThG suggestion in one instance I also used cristi_2112 idea for another seperate script.  Hopefully no one has a problem with me spliting the points.
0
All Courses

From novice to tech pro — start learning today.