Finding the nth row (SQL)...debative question

Reply

Join Date: Jan 2007
Posts: 16
Reputation: deostroll is an unknown quantity at this point 
Solved Threads: 0
deostroll's Avatar
deostroll deostroll is offline Offline
Newbie Poster

Finding the nth row (SQL)...debative question

 
0
  #1
Mar 6th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 28
Reputation: yerbol is an unknown quantity at this point 
Solved Threads: 0
yerbol's Avatar
yerbol yerbol is offline Offline
Light Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #2
Mar 6th, 2007
what do you mean by isolate?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 16
Reputation: deostroll is an unknown quantity at this point 
Solved Threads: 0
deostroll's Avatar
deostroll deostroll is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #3
Mar 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 28
Reputation: yerbol is an unknown quantity at this point 
Solved Threads: 0
yerbol's Avatar
yerbol yerbol is offline Offline
Light Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #4
Mar 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: sangramtheroot is an unknown quantity at this point 
Solved Threads: 1
sangramtheroot sangramtheroot is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #5
Mar 20th, 2007
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.


  1. SELECT * FROM Employee E1 WHERE
  2. (N-1) = (SELECT Count(DISTINCT(E2.Salary)) FROM Employee E2 WHERE
  3. E2.Salary > E1.Salary)

Sample structure of table is
  1. CREATE TABLE Employee
  2. (
  3. Eid int,
  4. Name varchar(10),
  5. Salary money
  6. )
Sample Values:
  1. INSERT INTO Employee VALUES (1,'name1',3500);
  2. INSERT INTO Employee VALUES (2,'name2',2500);
  3. INSERT INTO Employee VALUES (3,'name3',2500);
  4. INSERT INTO Employee VALUES (4,'name4',5500);
  5. INSERT INTO Employee VALUES (5,'name5',7500);
  6. INSERT INTO Employee VALUES (6,'name6',2400);

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

:cheesy:
Last edited by sangramtheroot; Mar 20th, 2007 at 3:41 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 6
Reputation: kmillen is an unknown quantity at this point 
Solved Threads: 0
kmillen kmillen is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #6
May 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: sangramtheroot is an unknown quantity at this point 
Solved Threads: 1
sangramtheroot sangramtheroot is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #7
May 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 6
Reputation: kmillen is an unknown quantity at this point 
Solved Threads: 0
kmillen kmillen is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #8
May 3rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: ashish841987 is an unknown quantity at this point 
Solved Threads: 0
ashish841987 ashish841987 is offline Offline
Newbie Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #9
Aug 24th, 2009
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

  1. SELECT emp_name FROM
  2. (
  3. SELECT emp_name, row_number() over(ORDER BY getdate()) as r
  4. FROM employee
  5. ) as t
  6. WHERE r = 5
Last edited by peter_budo; Aug 24th, 2009 at 5:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 34
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 11
huangzhi huangzhi is offline Offline
Light Poster

Re: Finding the nth row (SQL)...debative question

 
0
  #10
Aug 25th, 2009
May be this SQL can solve this problem:
  1. CREATE TABLE #tmpT (ABC char(1))
  2.  
  3. INSERT #tmpT
  4. SELECT 'A' union ALL
  5. SELECT 'B' union ALL
  6. SELECT 'C' union ALL
  7. SELECT 'D' union ALL
  8. SELECT 'E' union ALL
  9. SELECT 'F' union ALL
  10. SELECT 'G' union ALL
  11. SELECT 'H' union ALL
  12. SELECT 'I' union ALL
  13. SELECT 'J'
  14.  
  15. SELECT top 1 ABC
  16. FROM (SELECT top 3 ABC FROM #tmpT order by ABC) X
  17. ORDER BY ABC DESC
  18.  
  19. DROP TABLE #tmpT

If you want to get 5th record change 'top 3' become 'top 5'
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC