Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Top 100 spending customers

Posted on 2008-10-16
8
Medium Priority
?
307 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 66

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1500 total points
ID: 22764615
As I said: Exclude dbo.sent_items.date_order_placed
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

971 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