Groups | Blog | Home
all groups > sql server programming > august 2004 >

sql server programming : Go to specified row


Anith Sen
8/27/2004 3:48:32 PM
There is no such thing as "15th" row in a relational tables. In relational
databases, tables are sets which means rows have no order by themselves and
there should not be any duplicates.

Now, if you are interested in a specific value, say 15th largest value in a
column, you can do this by logically ordering the columns based on some
sequence of values and find it out. You can find out different options of
ranking the values in a particular column at :
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q186133

Using similar logic you could retrieve say for instance, the 15th smallest
value in a column by using :

SELECT *
FROM tbl t1
WHERE ( SELECT COUNT( * )
FROM tbl t2
WHERE t2.col <= t1.col ) = 15 ;

--
Anith

Eduardo Greco
8/27/2004 5:37:08 PM
Hi there...
I want to show a specific row in my table, say, the 15th row. But I want to
show only this row, not the previous rows.
How to do it?
Using SQL Server 2000.
Thank you.
Eduardo

Itzik Ben-Gan
8/27/2004 11:45:36 PM
Something to keep in mind when talking about row numbers (besides the fact
that some people will probably bash you for the notion of a row number in a
relational environment :-), is that you need to provide a deterministic sort
criteria (unique).
e.g., give me the 15th most recent order. Most recent is defined by
orderdate, which may have duplicates, so here you also need to specify a
tie-breaker, e.g., orderid.
So...

USE Northwind

SELECT TOP 1 *
FROM (SELECT TOP 15 *
FROM Orders
ORDER BY orderdate DESC, orderid ASC) AS D
ORDER BY orderdate, orderid

--
BG, SQL Server MVP
www.SolidQualityLearning.com


[quoted text, click to view]

Harag
8/28/2004 10:41:29 AM
On Fri, 27 Aug 2004 17:37:08 +0100, "Eduardo Greco"
[quoted text, click to view]


As the other 2 say, there is no such things as rows in a RMDB.

checkout

http://aspfaq.com/show.asp?id=2427

http://aspfaq.com/show.asp?id=2120

the second one I pointed you to is mainly for the very bottom method
of paging rows (which I'm sure you might ask one day) by Chris Hohmann
(i know this isn't a paging problem you got) but the way he reads the
rows is an alternative to the other 2 options given here by Itzik &
Anith.


CREATE PROCEDURE GetMemberRow(@RowID INT = 1)
AS
DECLARE @id INT

SET ROWCOUNT @RowID
SELECT @id = MemID
FROM YourTable
ORDER BY MemID

SET ROWCOUNT 1
SELECT MemID, MemName
FROM YourTable
ORDER BY MemID
GO



Not sure if the above would be any faster than the other methods as
I'm still a "noob" and learning.

If any guru thinks the above is wrong (or way toooooo slow) then
Please let me know.

HTH

Harag
8/28/2004 11:06:29 AM
On Sat, 28 Aug 2004 10:41:29 +0100, Harag
[quoted text, click to view]

doh. row numbers! lol


Hugo Kornelis
8/28/2004 11:08:43 PM
[quoted text, click to view]

Hi Al,

I think the last query misses a WHERE clause:
WHERE MemID = @id
Otherwise, I don't see how this procedure will ever return anything other
than the row with the lowest MemID.

If that is added, than this can be a quick way to find and return the row
with the n-th lowest MemID, but only if MemID is the primary key of the
table.

Oh, and SET ROWCOUNT 1 isn't necessary either.

Best, Hugo
--

Harag
8/29/2004 10:59:36 AM
On Sat, 28 Aug 2004 23:08:43 +0200, Hugo Kornelis
[quoted text, click to view]

WHERE MemID = @id


[quoted text, click to view]


Correct, Doh! thanks for pointing that out.


[quoted text, click to view]

Yea, it would depend on what the "Order BY" clause is on, eg MemAge,
MemName etc. but as you say the PK is the key to search/sort on

[quoted text, click to view]

yea, in this case it isn't necessary. I was just mainly converting
Chris' Rowcount paging proc so left in the rowcount 1 to show the OP
which part of the proc its from, or the idea that the #1 could be
changes to a variable and that he could get the next X (3) members
after the 15th.

[quoted text, click to view]
AddThis Social Bookmark Button