Solved

Top 100 spending customers

Posted on 2008-10-16
8
304 Views
Last Modified: 2012-05-05
I'm currently try to build a page that will query the top 100 customers that have spent the most money between two dates that we enter in a form field. I've tried lots of different ways to get it to work but it just doesn't produce the numbers that I'm looking for :( The closest I've been able to get is where it pulls up the sum for the highest SINGLE order, but it doesn't seem to combine the orders by customer_ID.

I have two tables, one with all of the customer information, and one with the order information. Also it won't return 100 top rows even though it's stated to do so.


SELECT     TOP (100) SUM(dbo.TBL_OrderSummary.qty * dbo.TBL_OrderSummary.item_price) AS tot, dbo.sent_items.customer_ID, dbo.sent_items.customer_first,
                       dbo.sent_items.customer_last, dbo.sent_items.city, dbo.sent_items.state, dbo.sent_items.zip, dbo.sent_items.country, dbo.sent_items.email, 
                      dbo.sent_items.phone, dbo.sent_items.address, dbo.sent_items.address2, dbo.sent_items.company, dbo.sent_items.date_order_placed, 
                      dbo.sent_items.province
FROM         dbo.TBL_OrderSummary INNER JOIN
                      dbo.sent_items ON dbo.TBL_OrderSummary.InvoiceID = dbo.sent_items.ID
WHERE     (dbo.sent_items.shipped = N'Shipped')
GROUP BY dbo.sent_items.customer_ID, dbo.sent_items.customer_first, dbo.sent_items.customer_last, dbo.sent_items.city, dbo.sent_items.state, 
                      dbo.sent_items.zip, dbo.sent_items.country, dbo.sent_items.email, dbo.sent_items.phone, dbo.sent_items.address, dbo.sent_items.address2, 
                      dbo.sent_items.company, dbo.sent_items.date_order_placed, dbo.sent_items.province
ORDER BY tot DESC

Open in new window

0
Comment
Question by:twentythree
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 22737428
To query based on an aggregate (i.e. top 100 spenders, instead of top 100 spend records), use the HAVING clause instead of WHERE

SELECT TOP 100 Customer, Sum(Orders)
FROM YourTable
GROUP BY Customer
ORDER BY Sum(Orders) DESC

0
 
LVL 11

Expert Comment

by:Louis01
ID: 22737798

SELECT TOP 100 * FROM (
	SELECT SUM(dbo.TBL_OrderSummary.qty * dbo.TBL_OrderSummary.item_price) AS tot, dbo.sent_items.customer_ID, dbo.sent_items.customer_first,
	 dbo.sent_items.customer_last, dbo.sent_items.city, dbo.sent_items.state, dbo.sent_items.zip, dbo.sent_items.country, dbo.sent_items.email, 
	 dbo.sent_items.phone, dbo.sent_items.address, dbo.sent_items.address2, dbo.sent_items.company, dbo.sent_items.date_order_placed, 
	 dbo.sent_items.province
	FROM dbo.TBL_OrderSummary INNER JOIN
	 dbo.sent_items ON dbo.TBL_OrderSummary.InvoiceID = dbo.sent_items.ID
	WHERE (dbo.sent_items.shipped = N'Shipped')
	GROUP BY dbo.sent_items.customer_ID, dbo.sent_items.customer_first, dbo.sent_items.customer_last, dbo.sent_items.city, dbo.sent_items.state, 
	 dbo.sent_items.zip, dbo.sent_items.country, dbo.sent_items.email, dbo.sent_items.phone, dbo.sent_items.address, dbo.sent_items.address2, 
	 dbo.sent_items.company, dbo.sent_items.date_order_placed, dbo.sent_items.province
) t1
ORDER BY tot DESC

Open in new window

0
 

Author Comment

by:twentythree
ID: 22737813
This is outputting the same data as the one I had posted before. Basically it's not combining the sum of all the orders by customer_ID. It just seems to be pulling single large orders when what I'm trying to do is compile them by customer. Plus, for some odd reason, it never outputs exactly the top 100. Most of the time it's less than 20...
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 11

Expert Comment

by:Louis01
ID: 22737832
Does removing the following columns from the SELECT and GROUP BY clauses make a difference?
dbo.sent_items.customer_ID
dbo.sent_items.date_order_placed

Remove all columns you do not really need.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22746183
First.  Does this produce the right results:

SELECT      TOP 100
            s.customer_ID,
            SUM(o.qty * o.item_price) tot
FROM      dbo.sent_items s
            INNER JOIN dbo.TBL_OrderSummary o ON s.ID = o.InvoiceID
WHERE      s.shipped = N'Shipped'
GROUP BY
            s.customer_ID
ORDER BY
            tot DESC
0
 

Author Comment

by:twentythree
ID: 22760158
The problem that I seem to be having is the GROUP BY clause. I do need all the other fields to output like street, date_order_placed, etc but those seem to conflict with the other information.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22763096
>>I do need all the other fields to output like street, date_order_placed, etc but those seem to conflict with the other information.<<
Exactly.  Now does the query I posted previously give you the right number of rows.  I need to know that answer, before I can proceed to contribute any valid solution.
0
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 22764615
As I said: Exclude dbo.sent_items.date_order_placed
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 64
Server 2012 r2 and SQL 2014 6 34
SQL Server - Multiple Conditions on Left or RIGHT Joins 2 30
Begin Transaction 12 26
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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