Solved

2 queries and 1 array

Posted on 2006-07-14
10
304 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:iceman19330
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
 

Author Comment

by:iceman19330
Comment Utility
DB2 to MySQL and vice versa.  The DB is in Canada via VPN and the MySQL is local.
0
 
LVL 14

Accepted Solution

by:
ThG earned 300 total points
Comment Utility
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
 

Author Comment

by:iceman19330
Comment Utility
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
 
LVL 9

Assisted Solution

by:cristi_2112
cristi_2112 earned 200 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:ThG
Comment Utility
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
 
LVL 14

Expert Comment

by:ThG
Comment Utility
@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
 
LVL 9

Expert Comment

by:cristi_2112
Comment Utility
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
 
LVL 14

Expert Comment

by:ThG
Comment Utility
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
 

Author Comment

by:iceman19330
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now