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 |
prasad0482
Starting Member
12 Posts |
Posted - 2010-09-17 : 08:16:41
|
Hi good day,This is prasad.How to find Top 10 and Least 10 records with Totalselect a.cleintNoa.ClientNameb.amountfrom client_detail ainner join order bon a.clientNo=b.ClientNoselect SUM (amount) AS GrandTotal FROM Client_detail ********************************************************how to get top 10 and least 10 records using totalThanks in advance.Prasad.Bonthu |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-09-17 : 08:29:56
|
order it by that column and pick top 10. |
 |
|
prasad0482
Starting Member
12 Posts |
Posted - 2010-09-17 : 08:41:54
|
quote: Originally posted by sakets_2000 order it by that column and pick top 10.
then least 1o how to get i need it in single query, and it should show in results 20 records |
 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-09-17 : 08:44:03
|
order by column asc, and picking top 10 will give you least 10.order by column desc, and picking top 10 will give you greatest 10. |
 |
|
parody
Posting Yak Master
111 Posts |
Posted - 2010-09-20 : 08:45:44
|
and union all to get one result set... |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-09-20 : 12:25:22
|
deleted |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-09-21 : 08:24:39
|
YOu can't do select from order by union select from order by. The following doesn't solve your problem, as you provided no sample data, but you can adapt it. Hint: You can do this ,rank()over(order by sum(number) asc)declare @table table (col1 int identity(1,1),Number int)insert into @tableselect checksum(newid())from master..spt_values where type = 'P' and number between 1 and 1000SELECT Position,numberFROM(select 'Top10' as Position,number,rank()over(order by number desc) as Rankfrom @tableunionselect 'Bottom10' ,number,rank()over(order by number asc)from @table) awhere rank between 1 and 10JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|