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!
3 1146
If we assume your tables look like this (or similar): -
Table 'customers' Table 'orders'
-
==================== ============================
-
custID | custName ordID | ordPrice | custID
-
==================== ============================
-
1 John 1 15 5
-
1 Bill 2 10 4
-
1 Jake 3 200 4
-
1 Jimmy 4 220 4
-
1 Lane 5 10 3
-
6 12 3
-
7 5 2
-
8 6 5
-
9 110 1
-
10 100 1
-
Your query should appear something like this: - 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: -
==============================================
-
custID | custName | sumTotal | cntOrders
-
==============================================
-
1 John 210 2
-
1 Bill 5 1
-
1 John 22 2
-
1 John 430 3
-
1 John 21 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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 internet SELECT query's.
|
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 to learn, but now I'd like
to ask anyone who reads this, including Aaron, for some clarification. I
imagine others might benefit, too.
"Aaron Bertrand - MVP" <aaron@TRASHaspfaq.com> wrote
> A few suggestions.
> (3) why do you constantly set...
|
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 I am working on:
http://www3.telus.net/thothworks/LinLeastSqPoly4.html).
I provide a form for a user to enter up to twenty (M = 20) data pairs.
The user need not enter data for all twenty pairs, but
the user must indicate that data is present by...
|
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
|
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 how error logging can be
incorporated using a Sql Server log with an additional text log in case Sql
Server was down.
My inclination after reading the article is to use Try-Catch-Finally as
little as possible (tends to slow things if you have...
| |
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 string arrays, which are just lists of people or companies in
alphabetic order. These names may have accented and umlauted
characters (which are present as the plain ASCII - not as the entity
&# character). The page is UTF-8 encoded.
e.g. ...
|
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 string arrays, which are just lists of people or companies in
alphabetic order. These names may have accented and umlauted
characters (which are present as the plain ASCII - not as the entity
&# character). The page is UTF-8 encoded.
e.g. ...
|
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 key (c1, c2));
|
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 :
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |