Groups | Blog | Home
all groups > sql server mseq > september 2003 >

sql server mseq : User-defined function for taking the middle scores


Swami
9/7/2003 1:15:59 PM
Given a table that includes 2 digit test scores how can
you write a function that eliminates the three largest
and the three smallest values and returns all the values
Vishal Parkar
9/8/2003 5:14:33 AM
create table score(scr int, name varchar(8000))

insert into score values(10,'vishal')
insert into score values(20,'vishal')
insert into score values(20,'vishal')
insert into score values(30,'roger')
insert into score values(40,'Venkat')
insert into score values(50,'russ')
insert into score values(60,'ram')
insert into score values(70,'bala')
insert into score values(70,'danny')
insert into score values(80,'sean')

--Following query will give you rows excluding top 3 and bottom 3 scores
from the above table.

select distinct Y.*
from score Y join
(select scr ,(select count(distinct scr) from score where scr <= a.scr)
asc_rank,
(select count(distinct scr) from score where scr >= a.scr) desc_rank
from score a)X
on y.scr = x.scr and (asc_rank > 3 and desc_rank > 3)

--
-Vishal

AddThis Social Bookmark Button