Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a view with a bit column 1

Status
Not open for further replies.

coospaa

Programmer
May 1, 2003
33
SG
Hi, I have a table A with one decimal-typed column Score. Now I want to create a view with A.Score and an additional bit-type column as follows:
1 - if the score is ranked as top 20% high score;
0 - otherwise.

I tried to create a view as: select A.score, IsTop20Perc(A.score) from A;
with IsTop20Perc as a function as follows:
Set @Return = cast(case when exists
(select top(20) percent with ties score from A order by score desc) a where score=@score

It works but it takes very long to execute the view. Is there a way to optimize it?

Any thoughts are appreciated. Thanks!
 
Select *,isnull(Scores.Score,0,1)Top20Percent
From Tablea
left join(
select top(20) percent with ties score
from A
order by score desc
)Scores
on A.Score=Scores.Score
 
Thanks PWise! It works perfectly. I did one small modifiction: isnull only takes 2 parameters in t-sql, therefore i've replaced it with a case exp as follows:
(case when Score is null then 0 else 1 end) as bit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top