Solved

Query for top customer where total sales is maximum with paid

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 36
string fuctions 4 26
T-SQL Default value in Select? 5 27
Need help with a Stored Proc on Sql Server 2012 4 10
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
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.

777 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