how to display nth record from table
5 4324
Try this (Suppose I want to find the 6th record among 10th record) Sample data - declare @t table(id int identity, name varchar(10))
-
insert into @t
-
select 'name1' union all select 'name2' union all select 'name3' union all
-
select 'name4' union all select 'name5' union all select 'name6' union all
-
select 'name7' union all select 'name8' union all select 'name9' union all
-
select 'name10'
Solution 1:(SQL SERVER 2005+) - select x.id,x.name from (
-
select row_number()over(order by id) rn,t.* from @t t) x
-
where x.rn = 6
Solution 2:(SQL SERVER 6.5 +) - select x.id,x.name from (
-
select id,name ,(select COUNT(*) from @t t2 where t2.id <=t1.id) as rn
-
from @t t1)x where x.rn = 6
-
Output: Solution 3: With the help of a number table(SQL SERVER 6.5 +)
First create a number table - declare @numbertab table(rownums int)
-
insert into @numbertab
-
select distinct column_id from master.sys.all_columns order by 1 asc
Then fire the query - select id, name
-
from @numbertab n join @t t
-
on n.rownums = t.id
-
where n.rownums = 6
Hope this helps
There is no -
select nth record from table
-
You will have to think of a way to simulate it.
How you do that depends on a lot of things.
1) What version of SQL server do you have?
SQL server 2005 has a function that adds a row number field to a query.
ealier versions can do that through an auto number field in a temp table
2) Does any of your data naturally contain something you can use for
record sequencing.
For example a date field...like this to get the 10th record -
SELECT Top 1 Date,Other,Fields
-
FROM
-
( SELECT Top 10 Date,Other,Fields
-
FROM TheTable
-
)a
-
ORDER BY Date DESC
-
nbiswas posted while I was writing mine
Oh well you now have plenty of options to choose from
:)
- SELECT Top 1 Date,Other,Fields
-
FROM
-
( SELECT Top 10 Date,Other,Fields
-
FROM TheTable
-
)a
-
ORDER BY Date DESC
- SELECT Top 1 Date,Other,Fields
-
FROM
-
( SELECT Top 10 Date,Other,Fields
-
FROM TheTable
-
ORDER BY Date DESC
-
)a
-
this is probably the best solution for such a most commonly asked interview question ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dotyet |
last post by:
Hey All,
I have a small problem. consider the following
>db2 "create table ttt (mynum float, yournum double )"
DB20000I The SQL command completed successfully.
>db2 "describe table ttt"
...
|
by: Carl |
last post by:
I have a main form with navigation buttons on it and a label showing
for example Record 1 of 15 using recordsetclone on it and eveything
works fine. When I move through the records the record...
|
by: SJM |
last post by:
I have a problem that occurs occasionally with a db for a undetermined
reason which I would love to solve.
I construct and append a series of 7 records to a table using ADO
recordset. Each record...
|
by: bala |
last post by:
hi!!!
i need to display a disclaimer which is two page in length in a word
document. i also need to format the text. the idea is something as
follows
on opening the application, a form which...
|
by: Wadim Grasza |
last post by:
I want to store and display (on a form or a report) multiple pictures
per
record in an access database. The pictures are not stored within the
database. They are stored as files and the database...
|
by: M. David Johnson |
last post by:
I cannot get my OleDbDataAdapter to update my database
table from my local dataset table. The Knowledge Base
doesn't seem to help - see item 10 below.
I have a Microsoft Access 2000 database...
|
by: Greg |
last post by:
Hi,
I have a table with "dates", i'd like to display those dates on a calendar.
I've put a calendar in a form, linked to my "date" field, and it works, but
only showing one "date" per calendar....
|
by: Dan Somdahl |
last post by:
Hi, I am new to ASP but have what should be a fairly simple task that I
can't figure out.
I need to parse a string from a single, semi-colon delimited, 60 character
field (el_text) in a recordset...
|
by: seth_hickel |
last post by:
With other solutions I would get a recordset, read each record and
display data by formating my html as I wanted to display values of
each record. I am trying to display data in a three column...
|
by: phil67b |
last post by:
Hello everybody,
I have a page rech.php where I'm doing a multi-criteria research Ex.
choose your car model, choose your country.
After validation of my form, on the same page, the lines will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |