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
 Getting the n-th record of a found set

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-09-25 : 08:50:29
I have this table that describes the state of production of an item on its way through the different workshops: (this example reflects just the records for one ProdID (item) with 4 workshops involved; but there are items that are built in up to 10 workshops.
ProdID Level OPRID Workshop State
----- ----- ----- ------ -----
WO1201 4 10 20012 1
WO1201 3 20 20011 0
WO1201 2 30 20051 0
WO1201 1 40 20031 0
I'm looking for an expression that returns the state of the N-th workshop. The order is described by ascending OPRID (or descending Level)

What I did (-until I found out that OPRID is not always 10,20,30... but can be 15)
Select .., (select Workshop +'('+ State +')'
from PRODROUTE
WHERE PRODROUTE.PRODID =PRODTABLE.PRODID and PRODROUTE.OPRID=10) as Prod1,
(select Workshop +'('+ State +')'
from PRODROUTE
WHERE PRODROUTE.PRODID =PRODTABLE.PRODID and PRODROUTE.OPRID=20) as Prod2,
... Prod10
from PRODTABLE
I even found a cool description http://blog.sqlauthority.com/2008/04/02/sql-server-find-nth-highest-salary-of-employee-query-to-retrieve-the-nth-maximum-value/
of how to get the N-th record, ordered by a certain criteria, but it doesn't return NULL in case that you are looking for the 7th Level, while there are only 4 records found.

Martin

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-09-25 : 09:07:49
Madhi to the rescue:
http://beyondrelational.com/modules/2/blogs/70/posts/10790/find-nth-maximum-value.aspx








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2012-09-25 : 11:35:15
Great!
Go to Top of Page
   

- Advertisement -