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
 General SQL Server Forums
 New to SQL Server Programming
 Selecting top 5 highest rows

Author  Topic 

olisajideofor
Starting Member

1 Post

Posted - 2006-09-25 : 06:02:50
I have a table stock, on the table I have company_name, stock_value, sector.
Now I want to get top five rows from this table depending on the five highest value of stock_value and for each sector. The query has to run on both oracle DB and Microsoft SQL Server with top priority MSSQL.
I will appreciate if you help me on this

jideofor

nr
SQLTeam MVY

12543 Posts

Posted - 2006-09-25 : 09:06:20
select *
from tbl t
where stock_value in (select top 5 t2.stock_value from tbl t2 where t2.sector = t.sector order by t2.stock_value desc)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-25 : 09:11:40
Also refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx


Madhivanan

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

- Advertisement -