[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Select by rownumber

Posted on 2013-05-10
8
Medium Priority
?
301 Views
Last Modified: 2013-05-10
Hello,
I would like to find a record by rownumber. How do I do that?

DECLARE @TempTable table(int id)
INSERT INTO @TempTable(id)value(11)
INSERT INTO @TempTable(id)value(151)
INSERT INTO @TempTable(id)value(17)

I would like to select row 2, i.e. get 151
0
Comment
Question by:johnson1
[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
8 Comments
 
LVL 6

Assisted Solution

by:BurundiLapp
BurundiLapp earned 500 total points
ID: 39155275
0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 500 total points
ID: 39155289
This might work:

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNum, ID FROM @TempTable) sub
WHERE RowNum = 2
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155294
row_number() over ... applies to the retrieved rowset. You can't address the 2nd row ever inserted directly with it.
You will need a numeric column with the IDENTY property set. That column will auto-increment on each INSERT (only), and can then be selected for.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 1000 total points
ID: 39155320
Here is an Example....

http://sqlfiddle.com/#!3/89cd0/10

Create Table TempTable 
(id int)

INSERT INTO TempTable(id)values(11);
INSERT INTO TempTable(id)values(151);
INSERT INTO TempTable(id)values(17);

Open in new window


Select 
* 
From 
(select 
ID,
ROW_NUMBER() over (order by (SELECT 1)) AS rownumber
from temptable
) as t
where rownumber = 2

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155339
The order of rows is not static, so that is not reliable. If there is an index, or even without, the order might change with later inserts.
0
 
LVL 7

Accepted Solution

by:
Ross Turner earned 1000 total points
ID: 39155369
Wouldn't it make more sense to add an identity field

http://sqlfiddle.com/#!3/6a8bb/1


Create Table TempTable 
(ident int identity,
  id int)

INSERT INTO TempTable(id)values(11);
INSERT INTO TempTable(id)values(151);
INSERT INTO TempTable(id)values(17);

Open in new window


Select * from Temptable
where ident = 2

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155382
Ross,
see http:#a39155294 ;-).
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39155387
lol yeah what you said :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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