Solved

Top 100 spending customers

Posted on 2008-10-16
8
298 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

895 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

16 Experts available now in Live!

Get 1:1 Help Now