Solved

Query for top customer where total sales is maximum with paid

Posted on 2014-03-30
1
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 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