DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   Finding the nth row (SQL)...debative question (http://www.daniweb.com/forums/thread71704.html)

deostroll Mar 6th, 2007 3:52 am
Finding the nth row (SQL)...debative question
 
Is there a way to isolate the nth record from a table using sql queries alone? The table is a non relational one and you cannot use any temporary tables...Do u think there is a possible solution for this in MS SQL Server 2000? Otherwise tell me whatever you know?

PS: I believe this is the right place to post my question...

yerbol Mar 6th, 2007 12:18 pm
Re: Finding the nth row (SQL)...debative question
 
what do you mean by isolate?

deostroll Mar 7th, 2007 2:28 pm
Re: Finding the nth row (SQL)...debative question
 
Is it possible to get that record as the only record in the record-set?

PS: I hope you know what a result-set is...? It is just the records returned after executing a select query.

yerbol Mar 7th, 2007 2:38 pm
Re: Finding the nth row (SQL)...debative question
 
First I would advise you to have a unique numerical primary key , that's usually the easiest way to return a record with some particular number.

sangramtheroot Mar 20th, 2007 3:37 am
Re: Finding the nth row (SQL)...debative question
 
Here is example sql query for you.

put any integer value in place of N
e.g for first record N=1;
for 2nd record N=2; and so on....

Remember it is not optimized at this time. You may want to optimize it.


Select * From Employee E1 Where
    (N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
        E2.Salary > E1.Salary)

Sample structure of table is
Create table Employee
(
Eid int,
Name varchar(10),
Salary money
)
Sample Values:
Insert into Employee values (1,'name1',3500);
Insert into Employee values (2,'name2',2500);
Insert into Employee values (3,'name3',2500);
Insert into Employee values (4,'name4',5500);
Insert into Employee values (5,'name5',7500);
Insert into Employee values (6,'name6',2400);

This query is for Salary criteria. If u want anything else just replace.

:cheesy:

kmillen May 2nd, 2007 12:51 pm
Re: Finding the nth row (SQL)...debative question
 
You shouldn't rely on a specific record being in a specific location in a DBMS (unless you manage the database and its order); no database I know of guarrantees this. Seeking this type of solution tells me that your records are not defined properly to warrant individual selection regardless of their physical location in the database.

Data extraction regardless of physical order IS the power that databases offer.

sangramtheroot May 3rd, 2007 1:39 am
Re: Finding the nth row (SQL)...debative question
 
Well i must agree to you but improvisation can be done and I just give here is the sample to understand logic behind it...
I must bound to give solution which is generic in nature.
Also enterprise databases are indexed and managed in order to speed up the thing (I agree that this might not be 100% true) .
Sticking to solution I want to say that the whole thing is based on result of the query and clauses used filter result and regardless of how the DBMS stores data.
I must say that you should emphasis on query and logic not on DBMS structure.

kmillen May 3rd, 2007 10:36 am
Re: Finding the nth row (SQL)...debative question
 
Well, I definitely have no problem with finding the logic and or a generic solution to a problem. Problem solving is why I became a software engineer, but concerning this particular problem, I believe the actions of the DBMS are important.

Attempting to extract a record based on physical location is a deviation from the very spirit of relational database use. And while it is true that indexing may improve performance if done properly, there are many ways to improve record access without physical ordering.

SQL is optimized to extract data based on field values; call me a purist, but a departure from that is a departure from the power of the DBMS.

I did like your solution though sangramtheroot.

ashish841987 Aug 24th, 2009 4:59 am
Re: Finding the nth row (SQL)...debative question
 
suppose we have a table employee and inside it there is only 1 col emp_name... we r getting 5th row/emp_name in following example...

this only works in sqlserver 2005...not in 2000

select emp_name from
(
select emp_name, row_number() over(order by getdate()) as r
  from employee
) as t
where r = 5

huangzhi Aug 25th, 2009 12:10 am
Re: Finding the nth row (SQL)...debative question
 
May be this SQL can solve this problem:
create table #tmpT (ABC char(1))

insert #tmpT
  select 'A' union all
  select 'B' union all
  select 'C' union all
  select 'D' union all
  select 'E' union all
  select 'F' union all
  select 'G' union all
  select 'H' union all
  select 'I' union all
  select 'J'

select top 1 ABC
  from (select top 3 ABC from #tmpT order by ABC) X
  order by ABC desc

drop table #tmpT

If you want to get 5th record change 'top 3' become 'top 5'


All times are GMT -4. The time now is 5:34 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC