| Author |
Topic |
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-10-06 : 14:37:10
|
| I would like to use the top argument to return the top 5 of a particular group, and would than like to take the bottom 95 (basically group all the remaining records) and display those and its summed market value as lets say other, so have it look like the following100 Accounts Total market value $1000result set i want to look like this:account1 $200account2 $150account3 $125account4 $100account5 $75other(accounts 6-100) $350Hope this makes sense. |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-10-06 : 17:49:15
|
I'd be interested to know if anyone can come up with a better solution than this. I had to create two views and use them with a UNION because SQL Server would not allow the UNION between the two SELECTs. But this workscreate table AccTable(account char(5), value int)GOinsert AccTable values ('acc1', 100)insert AccTable values ('acc2', 400)insert AccTable values ('acc3', 200)insert AccTable values ('acc4', 800)insert AccTable values ('acc5', 300)insert AccTable values ('acc6', 800)insert AccTable values ('acc7', 400)insert AccTable values ('acc8', 900)insert AccTable values ('acc9', 50)insert AccTable values ('acc10', 200)GOCREATE VIEW Agg1ASSELECT TOP 5 t1.account, t1.valueFROM AccTable t1ORDER BY t1.value DESCGOCREATE VIEW Agg2ASSELECT 'other' account, sum(t2.value) valueFROM AccTable t2WHERE t2.account NOT IN (SELECT TOP 5 t3.accountFROM AccTable t3ORDER BY t3.value DESC)GOCREATE VIEW AccAggASselect * from Agg1union allselect * from Agg2GOSELECT * FROM AccAgg |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-06 : 22:40:08
|
Without the views.select c.Account, c.Valuefrom ( select a.* from ( select top 5 t1.account, t1.value, Type = 1 from acctable t1 order by t1.value desc ) a union all select b.* from ( select 'other' account, sum(t2.value) value, Type = 2 from acctable t2 where t2.account not in ( select top 5 t3.account from acctable t3 order by t3.value desc ) ) b ) corder by c.Type, c.Value desc, c.Account Results:Account Value ------- ----------- acc8 900acc4 800acc6 800acc2 400acc7 400other 850(6 row(s) affected) CODO ERGO SUM |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-06 : 23:13:24
|
Without a union.select Account = case when b.account is null then 'Other' else a.account end, Value = sum(a.value)from acctable a left join ( select top 5 t3.account, t3.value from acctable t3 order by t3.value desc ) b on a.Account = b.Accountgroup by case when b.account is null then 2 else 1 end, case when b.account is null then 'Other' else a.account endorder by case when b.account is null then 2 else 1 end, sum(a.value) desc, case when b.account is null then 'Other' else a.account end Results:Account Value ------- ----------- acc8 900acc4 800acc6 800acc2 400acc7 400Other 850(6 row(s) affected) CODO ERGO SUM |
 |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-10-08 : 12:22:46
|
| I appreciate all the responses, and will give it a shot on Monday, thanks again. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-10-08 : 13:18:54
|
Here's a variation using a ranking subquery rather than left joining to top 5:select account = case when rank > 5 then 'Other' else account end ,sum(value) valuefrom ( select account ,rank = (select count(*) from Acctable where value >= a.value) ,value from AccTable a ) agroup by case when rank > 5 then 'other' else account end ,case when rank > 5 then 2 else 1 endorder by case when rank > 5 then 2 else 1 end ,value desc Be One with the OptimizerTG |
 |
|
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-10-09 : 09:32:59
|
| I have the following now:select company = case when b.company is null then 'Other' else a.company end, value = sum(a.accountmv)fromsnapsraw a left join ( select top 5 c.company, c.accountmv from snapsraw c order by c.accountmv desc) bon a.company = b.companywhere branchstate = 'Arizona' and monthend = '08/01/2006'group by case when b.company is null then 2 else 1 end,case when b.company is null then 'Other' else a.company endorder by case when b.company is null then 2 else 1 end,sum(a.accountmv) desc, case when b.company is null then 'Other' else a.company endThe results I get are only the other company market value and doesnt include the top 5? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-09 : 09:55:04
|
| >>Here's a variation using a ranking subquery rather than left joining to top 5:I think this approach will work correctly only if values are distinctMadhivananFailing to plan is Planning to fail |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-09 : 11:52:18
|
quote: Originally posted by duhaas I have the following now:select company = case when b.company is null then 'Other' else a.company end, value = sum(a.accountmv)fromsnapsraw a left join ( select top 5 c.company, c.accountmv from snapsraw c order by c.accountmv desc) bon a.company = b.companywhere branchstate = 'Arizona' and monthend = '08/01/2006'group by case when b.company is null then 2 else 1 end,case when b.company is null then 'Other' else a.company endorder by case when b.company is null then 2 else 1 end,sum(a.accountmv) desc, case when b.company is null then 'Other' else a.company endThe results I get are only the other company market value and doesnt include the top 5?
You have new conditions that you did not put in you original post.Since you did not explain your true requirements, the responses do no take those into account.CODO ERGO SUM |
 |
|
|
|