Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.
-
Michelle #1
to get the nth record meeting the query requirement
In SQL server, if I want to retrieve the first record which meets the query,
I can use 'select top 1'. However if I want to query the nth record, is
there a easy way to do it?
Thanks.
USE pubs
SELECT TOP 1 pub_id
FROM titles
WHERE type ='business'
ORDER BY pub_id
Michelle Guest
-
Interesting SQL query requirement for <SELECT> menu
Hi All Wondered if you could help me with the below query. I have 1 simple table called STOCKCATS that consists of 2 fields. These fields... -
[MEETING] CORE Meeting Oct. 25th
CORE (COlorado Ruby Enthusiasts) will be having a gathering at Paris on the Platte in Denver on October 25th (Saturday) starting at 4pm. No talks... -
Last record of query
Is there a way no figure out if I am at the last record of a MySQL query? Jesper -
[MEETING] CORE Meeting August 9th
Hi all, CORE (COlorado Ruby Enthusiasts) will be having a gathering at Paris on the Platte in Denver on August 9th (Saturday) starting at 4pm. ... -
Adding a new record when the record source is a query.
Hello All, I have a form that runs a query. The reason it runs on a query is because prior to it opening I have a form from which you can pick... -
Stu #2
Re: to get the nth record meeting the query requirement
Would something like this work? replace n with the required record.
select TOP 1 pub_id from ()> SELECT TOP n pub_id
> FROM titles
> WHERE type ='business'
> ORDER BY pub_id
ORDER BY pub_id desc
Cheers
Stu
Stu Guest
-
Anith Sen #3
Re: to get the nth record meeting the query requirement
If you meant the nth largest/smallest value in the dataset, you can try :
SELECT pub_id
FROM titles
WHERE type = 'business'
AND (SELECT COUNT(t1.pub_id)
FROM titles t1
WHERE t1.type = titles.type
AND t1.pubid <= titles.pubid) = @n
If you need to handles ties, you have to use COUNT(DISTINCT pub_id) in your
subquery.
--
- Anith
( Please reply to newsgroups only )
Anith Sen Guest
-
Vishal Parkar #4
Re: to get the nth record meeting the query requirement
Try this:
USE pubs
declare @x int
set @x=7 --get 7th record on the basis of order of pub_id
select rank, pub_id from
(SELECT (select count(distinct pub_id) from titles where pub_id <= a.pub_id)
rank, pub_id
FROM titles a
WHERE type ='business') X
where rank=@x
-Vishal
"Michelle" <Michelle@yahoo.com> wrote in message
news:ONo1AJxPDHA.1336@TK2MSFTNGP11.phx.gbl...query,> In SQL server, if I want to retrieve the first record which meets the> I can use 'select top 1'. However if I want to query the nth record, is
> there a easy way to do it?
> Thanks.
>
> USE pubs
> SELECT TOP 1 pub_id
> FROM titles
> WHERE type ='business'
> ORDER BY pub_id
>
>
>
Vishal Parkar Guest
-
Vishal Parkar #5
Re: to get the nth record meeting the query requirement
USE pubs
SELECT TOP 1 pub_id
FROM titles
WHERE type ='business'
ORDER BY pub_id
DESC --changed the clause to DESC to get the last record.
-Vishal
"Michelle" <Michelle@yahoo.com> wrote in message
news:#evxGMxPDHA.1336@TK2MSFTNGP11.phx.gbl...> or how can I retrieve the last record meeting the query? Thanks for your
> help.
>
> "Michelle" <Michelle@yahoo.com> wrote in message
> news:ONo1AJxPDHA.1336@TK2MSFTNGP11.phx.gbl...> query,> > In SQL server, if I want to retrieve the first record which meets the>> > I can use 'select top 1'. However if I want to query the nth record, is
> > there a easy way to do it?
> > Thanks.
> >
> > USE pubs
> > SELECT TOP 1 pub_id
> > FROM titles
> > WHERE type ='business'
> > ORDER BY pub_id
> >
> >
> >
>
Vishal Parkar Guest



Reply With Quote

