ghollis (3/19/2009)
Wouldn't using Top 25 WITH TIES accomplish the same thing, rather than using the CTE (assuming they are using the group by and order by clauses without the SalesCategory being included in the query)?
Yes, it does. Thank you for pointing out this option. This works well. Here's a test script ( I didn't bother including salesCategory in the table definition).....
create table #SalesTbl(
Salesperson char(30)
,SalesDollars money
,SalesDate datetime
)
insert #SalesTbl
Select 'Judy', 14554.22, '20090103' union All
Select 'Judy', 15854.22, '20090103' union All
Select 'Judy', 16554.22, '20090103' union All
Select 'Joyce', 26554.22, '20090103' union All
Select 'Ed', 25554.22, '20090103' union All
Select 'Andy', 24554.22, '20090103' union All
Select 'Len', 22222.22, '20090103' union All
Select 'Tom', 22222.22, '20090103' union All
Select 'Maria', 4554.22, '20090103' union All
Select 'Chris', 6654.22, '20090103' union All
Select 'Tony', 4554.22, '20090103' union All
Select 'Sally', 4654.22, '20090103' union All
Select 'Bob', 4554.22, '20090103' union All
Select 'Bob', 4554.22, '20090103'
Declare @StartDt Datetime
Declare @EndDt Datetime
Set @StartDt = '20090101'
Set @EndDt = '20090131'
Select top 5 with ties
Salesperson
,sum(salesDollars) TotSales
from #SalesTbl
Where SalesDate between @StartDt and @EndDt
group by SalesPerson
order by sum(salesDollars) Desc
and results:
Salesperson TotSales
------------------------------ ---------------------
Judy 46962.66
Joyce 26554.22
Ed 25554.22
Andy 24554.22
Len 22222.22
Tom 22222.22
(6 row(s) affected)