Solved

Query for top customer where total sales is maximum with paid

Posted on 2014-03-30
1
326 Views
Last Modified: 2014-03-30
trying to select a query which will display

eg. Select TOP 5 customers where

      - the totalamount is max , depending of the status = 'paid' of each invoice

and also the worst customer , totalamount min, and COUNT(*) where status='pending' or status = NULL
0
Comment
Question by:doramail05
1 Comment
 
LVL 12

Accepted Solution

by:
Koen Van Wielink earned 500 total points
ID: 39965760
For the top 5:

Select top 5 yourCustomerId, sum(totalAmount) as 'Amount'
from yourTable
Where invoiceStatus = 'Paid'
Order by sum(totalAmount) desc

The other queries work in a similar manner, just change your conditions in the where clause. For the top 5 min, change the sort order on sum(totalAmount) by removing "desc".
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 the fundamental information of how to create a table.

911 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