Solved

Query for top customer where total sales is maximum with paid

Posted on 2014-03-30
1
325 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

22 Experts available now in Live!

Get 1:1 Help Now