Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lols
Posting Yak Master
174 Posts |
Posted - 2009-05-08 : 00:25:48
|
| I want to find Top and Bottom 10% salesperson based on their sales. How to find?DECLARE @Sales TABLE ( SalesPersonID varchar(10), TotalSales int)INSERT @SalesSELECT 1, 200 UNION ALLSELECT 2, 300 UNION ALLSELECT 7, 300 UNION ALLSELECT 4, 100 UNION ALLSELECT 5, 600 UNION ALLSELECT 5, 600 UNION ALLSELECT 2, 200 UNION ALLSELECT 5, 620 UNION ALLSELECT 4, 611 UNION ALLSELECT 3, 650 UNION ALLSELECT 7, 611 UNION ALLSELECT 9, 650 UNION ALLSELECT 3, 555 UNION ALLSELECT 9, 755 UNION ALLSELECT 8, 650 UNION ALLSELECT 3, 620 UNION ALLSELECT 5, 633 UNION ALLSELECT 6, 720 GO |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-08 : 00:41:43
|
[code]select top 10 percent * from @Sales order by TotalSales descselect top 10 percent * from @Sales order by TotalSales asc[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2009-05-08 : 00:44:40
|
| I want to use CTE and also add a department column. can i write same query to find top 10% and bottom 10% in each department? I |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-08 : 01:36:57
|
Yes you can.;WITH YakAS (SELECT *, ntile(10) over (order by totalsales) as a, ntile(10) over (order by totalsales desc) AS b)SELECT * from yak where 1 in (a, b) E 12°55'05.63"N 56°04'39.26" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-08 : 01:59:09
|
quote: Originally posted by Peso Yes you can.;WITH YakAS (SELECT *, ntile(10) over (order by totalsales) as a, ntile(10) over (order by totalsales desc) AS b)SELECT * from yak where 1 in (a, b) E 12°55'05.63"N 56°04'39.26"
In SQLTeam, You are the one who mostly use Value in (col1,col2,...) syntaxYou can add your opinion in this posthttp://sqlblog.com/blogs/denis_gobo/archive/2009/04/09/13186.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|