Solved

2 queries and 1 array

Posted on 2006-07-14
10
305 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 look for a specific file type in a local or remote server directory using PHP.

919 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

19 Experts available now in Live!

Get 1:1 Help Now