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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Top 5 and Bottom 95

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 following

100 Accounts Total market value $1000

result set i want to look like this:

account1 $200
account2 $150
account3 $125
account4 $100
account5 $75
other(accounts 6-100) $350

Hope 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 works

create table AccTable
(account char(5), value int)
GO
insert 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)
GO

CREATE VIEW Agg1
AS
SELECT TOP 5 t1.account, t1.value
FROM AccTable t1
ORDER BY t1.value DESC
GO
CREATE VIEW Agg2
AS
SELECT 'other' account, sum(t2.value) value
FROM AccTable t2
WHERE t2.account NOT IN (SELECT TOP 5 t3.account
FROM AccTable t3
ORDER BY t3.value DESC)
GO
CREATE VIEW AccAgg
AS
select * from Agg1
union all
select * from Agg2
GO

SELECT * FROM AccAgg


Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-10-06 : 22:40:08
Without the views.

select
c.Account,
c.Value
from
(
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
) c
order by
c.Type,
c.Value desc,
c.Account

Results:

Account Value
------- -----------
acc8 900
acc4 800
acc6 800
acc2 400
acc7 400
other 850

(6 row(s) affected)



CODO ERGO SUM
Go to Top of Page

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.Account
group by
case when b.account is null then 2 else 1 end,
case when b.account is null then 'Other' else a.account end
order 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 900
acc4 800
acc6 800
acc2 400
acc7 400
Other 850

(6 row(s) affected)




CODO ERGO SUM
Go to Top of Page

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.
Go to Top of Page

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) value
from (
select account
,rank = (select count(*) from Acctable where value >= a.value)
,value
from AccTable a
) a
group by case when rank > 5 then 'other' else account end
,case when rank > 5 then 2 else 1 end
order by case when rank > 5 then 2 else 1 end
,value desc


Be One with the Optimizer
TG
Go to Top of Page

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)

from

snapsraw a left join ( select top 5 c.company, c.accountmv from snapsraw c order by c.accountmv desc) b

on a.company = b.company

where 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 end

order 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 end

The results I get are only the other company market value and doesnt include the top 5?
Go to Top of Page

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 distinct

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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)

from

snapsraw a left join ( select top 5 c.company, c.accountmv from snapsraw c order by c.accountmv desc) b

on a.company = b.company

where 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 end

order 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 end

The 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
Go to Top of Page
   

- Advertisement -