SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to generate Quartile ranking of the sales force?

Execute the following SQL Server T-SQL example script in Management Studio Query Editor to create a Quartile ranking of the sales staff of AdventureWorks Cycles.

-- SQL Quartile - ntile - NTILE(4) - SQL ranking functions

-- SQL inner join - SQL format money
USE AdventureWorks2008;
 
SELECT SalesStaff = p.LastName + ', ' + p.FirstName,
       NTILE(4) OVER(ORDER BY SalesYTD DESC) AS 'Quartile',
       YTDSalesAmount = '$' + convert(VARCHAR,s.SalesYTD,1),
       a.City,
       State = sp.StateProvinceCode
FROM   Sales.SalesPerson s
       INNER JOIN Person.Person p
         ON s.BusinessEntityID = p.BusinessEntityID
       INNER JOIN Person.Address a
         ON a.AddressID = p.BusinessEntityID
       INNER JOIN Person.StateProvince sp
         ON sp.StateProvinceID = a.StateProvinceID
WHERE  s.TerritoryID IS NOT NULL
       AND SalesYTD > 0;
GO
/*
SalesStaff     Quartile YTDSalesAmount    City       State
Mitchell, Linda   1     $5,200,475.23     Issaquah    WA
Pak, Jae          1     $5,015,682.38     Renton      WA
Blythe, Michael   1     $4,557,045.05     Issaquah    WA
Carson, Jillian   1     $3,857,163.63     Issaquah    WA
Varkey, Ranjit    2     $3,827,950.24     Renton      WA
Campbell, David   2     $3,587,378.43     Renton      WA
Saraiva, José     2     $3,189,356.25     Renton      WA
Ito, Shu          3     $3,018,725.49     Renton      WA
Reiter, Tsvi      3     $2,811,012.72     Issaquah    WA
Valdez, Rachel    3     $2,241,204.04     Renton      WA
Mensa-Annan, Tete 4     $1,931,620.18     Renton      WA
Vargas, Garrett   4     $1,764,938.99     Issaquah    WA
Tsoflias, Lynn    4     $1,758,385.93     Renton      WA

*/

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.