Solved

Top 100 spending customers

Posted on 2008-10-16
8
295 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 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now