473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What's the best (fastest to execute) way to handle this?

I need to put together a report that will pull data from the following tables: Customers, Orders.

The Customers table contains about 11,000 customer records and the Orders table contains about 40,000 order records.

What I need to do is basically pull a total number of orders and the total revenue per customer for a given date range.

The way it works now is it lists all customers and then it gets a COUNT for all orders for that particular customer and then SUM of their totals on each line. So I basically first query the Customers table, then Loop through the entire recordset while pulling COUNT and SUM for each line.

Example:
John Doe ABC Company 3 $350.00
Jane Doe XYZ Company 5 $900.00

The way it's written right now takes an enormously long amount of time and probably server resources. Does anyone know of a better, faster way to pull all this data into a report using ASP and HTML? Or is there another alternative?

Thanks in advance!
Dec 6 '07 #1
3 1132
Nicodemas
164 Expert 100+
If we assume your tables look like this (or similar):

Expand|Select|Wrap|Line Numbers
  1. Table 'customers'                Table 'orders'
  2. ====================             ============================ 
  3. custID | custName                ordID | ordPrice | custID
  4. ====================             ============================ 
  5. 1           John                 1       15          5
  6. 1           Bill                 2       10          4
  7. 1           Jake                 3       200         4
  8. 1           Jimmy                4       220         4
  9. 1           Lane                 5       10          3
  10.                                  6       12          3
  11.                                  7       5           2
  12.                                  8       6           5
  13.                                  9       110         1
  14.                                  10      100         1
  15.  
Your query should appear something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT c.custID, c.custName, SUM(ordPrice) as sumTotal, COUNT(ordID) as cntOrders FROM customers c left join orders o on c.custID = o.custID GROUP BY c.custID, c.custName
Which would produce results like:

Expand|Select|Wrap|Line Numbers
  1. ==============================================
  2. custID | custName | sumTotal | cntOrders
  3. ==============================================
  4. 1         John        210         2
  5. 1         Bill        5           1
  6. 1         John        22          2
  7. 1         John        430         3
  8. 1         John        21          2
  9.  
Dec 6 '07 #2
Sorry I should have posted the table structure...

The tables look like this:


Table 'customers'
================================================== ==
CustID | CustLName | CustFName | CustCompany
================================================== ==
1 John Smith ABC Company
2 Jane Doe XYZ Company
3 Jake Smith
4 John Johnson Anything Inc


Table 'orders'
===================================
OrderID | CustID | OrderTotal
===================================
1 1 25
2 1 45
3 2 15
4 3 18
5 3 23
6 3 75
7 2 40
8 2 25
9 4 105


I need to sort the results by CustLName, CustFname, CustCompany and then display it in this format:

================================================== ========
CustLName, CustFName | custCompany | sumTotal | cntOrders
================================================== ========
Smith, John ABC Company 70.00 2
Doe, Jane XYZ Company 80.00 3
Smith, Jake 116.00 3
Johnson, John Anything Inc 105.00 1
Dec 6 '07 #3
Actually I figured it out myself. Here is the SELECT statement in case anybody ever needs it.

SELECT c.CustID, c.CustLName, c.CustFName, c.CustCompany, SUM(OrderTotal) as sumTotal, COUNT(OrderID) as cntOrders FROM Customer c LEFT JOIN Orders o on c.CustID = o.CustID WHERE "& MY_SQL_QUERY &" GROUP BY c.CustCompany, c.CustLName, c.CustFName, c.CustID ORDER BY c.CustCompany, c.CustLName, c.CustFName
Dec 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: martijn | last post by:
H!, I'm testing things with Python with databases. But I have one big question. What is the 'fastest' database for the internet in combination with Python ? - with +/- 15 GB data. - fast...
37
by: middletree | last post by:
Yesterday, I posted a problem which, by the way, I haven't been able to solve yet. But in Aaron's reply, he questioned why I did several things the way I did. My short answer is that I have a lot...
4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: Sandy | last post by:
Hello - I read an interesting article on the web wherein the author states he doesn't handle too many errors at page level, but handles them at the application level. He further goes on to show...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
5
by: Dominik Czechowski | last post by:
Hi everyone! I have two tables, T1 and T2 defined as follows: create table T1(c1 integer not null, primary key (c1)); create table T2(c1 integer not null, c2 varchar(100) not null, primary...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.