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 prepare a sales report by staff?

Execute the following Microsoft SQL Server T-SQL script in Query Editor to create a sales report by sales staff. The report includes 2 ranking columns.

use AdventureWorks2008;

 

select  p.LastName +', '+p.FirstName as SalesPerson,

  count(distinct soh.SalesOrderID) as NoOfOrders,

  '$'+convert(varchar,convert(money,sum(sod.LineTotal)),1) as TotalSales,

   row_number() over ( order by sum(sod.LineTotal) desc ) as SalesOrderRank,

   row_number() over ( order by count(distinct soh.SalesOrderID) desc ) as NoOfOrdersRank

from Sales.SalesOrderHeader soh

  inner join Sales.SalesOrderDetail sod

  on soh.SalesOrderID=sod.SalesOrderID

  inner join Person.Person p

  on soh.SalesPersonID =p.BusinessEntityID

group by p.BusinessEntityID , p.LastName +', '+p.FirstName

order by sum(sod.LineTotal) desc
/*
SalesPerson       NoOfOrders  TotalSales      Rank    NoOfOrdersRank
Mitchell, Linda   418         $10,367,007.43    1     4
Carson, Jillian   473         $10,065,803.54    2     1
Blythe, Michael   450         $9,293,903.00     3     2
Pak, Jae          348         $8,503,338.65     4     5
Reiter, Tsvi      429         $7,171,012.75     5     3
Ito, Shu          242         $6,427,005.55     6     7
Saraiva, José     271         $5,926,418.36     7     6
Varkey, Ranjit    175         $4,509,888.93     8     10
Campbell, David   189         $3,729,945.35     9     9
Vargas, Garrett   234         $3,609,447.21     10    8
Ansmane, Pamela   95          $3,325,102.59     11    14
Mensa-Annan, Tete 140         $2,312,545.69     12    11
Valdez, Rachel    130         $1,827,066.71     13    12
Tsoflias, Lynn    109         $1,421,810.92     14    13
Jiang, Stephen    48          $1,092,123.86     15    15
Alberts, Amy      39          $732,759.18 16    16
Abbas, Syed       16          $172,524.45 17    17

*/

 

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.