Solved

select the nth row of a table

Posted on 2013-01-09
12
483 Views
Last Modified: 2013-01-13
how can i easily to select the nth row of a table in SQL query.

For example, if i want to select the 3rd of a table, how can i do that?
(notes: if the table does not have row number)
0
Comment
Question by:hongclub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 2

Expert Comment

by:thombeck
ID: 38760423
you can use this code:

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,
    columns
  FROM tablename
) AS foo
WHERE rownumber <= n
0
 
LVL 12

Expert Comment

by:Jared_S
ID: 38760429
use row_number()
post some details of your data and we can build the query.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 38760434
SQL 2005 and above has this feature built-in. Use the ROW_NUMBER() function. It is excellent for web-pages with a << Prev and Next >> style browsing:

Syntax:

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY MyColumnToOrderBy) AS RowNum, * FROM Table_1) sub
WHERE RowNum = 23

Taken fromhttp://stackoverflow.com/questions/16568/how-to-select-the-nth-row-in-a-sql-database-table

I use it all the time: had link bookmarked
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38760457
@cs97jjm3's method is excellent, I use it myself for pagination.  But... SQL tables do not have built-in 'row numbers' like spreadsheets do.  ROW_NUMBER() Depends on the ORDER BY statement for it's order.  If you insert a value that changes the ORDER BY, you will change the Row Number for a particular item.
0
 

Author Comment

by:hongclub
ID: 38760463
i used that too, but cuz in my sql has select distinct, so when i use the ROW_NUMBER, it return more rows than i though

select Distinct col1, ROW_NUMBER() Over(Order By col1), col2from #tempResult  order by col2 asc
0
 
LVL 41

Expert Comment

by:ralmada
ID: 38760505
maybe you should try with dense_rank

dense_rank() Over(Order By col1)

More info on dense_rank here:

http://msdn.microsoft.com/en-us/library/ms173825.aspx
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 38761410
i used that too, but cuz in my sql has select distinct, so when i use the ROW_NUMBER, it return more rows than i though
Try it this way:
SELECT  col1,
        col2,
        ROW_NUMBER() OVER (ORDER BY col1) Row
FROM    (SELECT DISTINCT
                col1,
                col2
         FROM   #tempResult
        ) d
WHERE Row = 3
ORDER BY col2 ASC

Open in new window

0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 38761785
In 2005/2008

SELECT  col1,
        col2,
        ROW_NUMBER() OVER (ORDER BY col1) Row
FROM    (SELECT DISTINCT
                col1,
                col2
         FROM   #tempResult
        ) d
WHERE Row = 3
ORDER BY col2 ASC

As Above solutions provided by other experts

In SQL Server 2012

select * from sys.objects where modify_date > format(DateAdd(DAy, -1, getdate()),'yyyyMMdd')
order by name
Offset 10 rows Fetch Next 1 rows only
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38765861
To clarify:  The comment awarded points is an exact copy and paste of the solution I posted previously here http:#a38761410
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38769175
Please leave your recommendations here as to how you wish to see this question closed.
3) http:#a38761410
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question