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 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-01-31 : 02:19:18
|
| I have a list of items to count, I only want to select top 5 decending, while the balance will be sum up.Item TotalBottle 10Pen 8Paper 7Book 5Eraser 4Pencil 3Ruler 3Red Pen 1Should it be like this?SELECT top 5 item, total from tableAHow can I sum up the balance to be named as 'Others'?Item TotalBottle 10Pen 8Paper 7Book 5Eraser 4Others 7 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-01-31 : 03:55:09
|
| Hmm..Anybody? |
 |
|
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2012-01-31 : 04:03:38
|
| create table #t1(Item varchar(10), Total float)insert into #t1select 'Bottle', 10unionselect 'Pen', 8unionselect 'Paper', 7unionselect 'Book', 5unionselect 'Eraser', 4unionselect 'Pencil', 3unionselect 'Ruler', 3unionselect 'Red Pen', 1select * from #t1select Top 5 * into #t2 from #t1 order by Total desc select * from #t2union allselect 'Others', SUM(Total) from #t1 where Item not in (select Item from #t2)SQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
|
|
|
|