Solved

2 queries and 1 array

Posted on 2006-07-14
10
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17108148
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
ID: 17108175
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
ID: 17108907
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:iceman19330
ID: 17109230
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
ID: 17109339
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
 
LVL 14

Expert Comment

by:ThG
ID: 17111588
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
ID: 17111599
@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
ID: 17114628
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
ID: 17115032
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
ID: 17325909
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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