Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Query Nth Row
Message
 
 
À
02/09/2005 14:35:02
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Titre:
Divers
Thread ID:
01046354
Message ID:
01046383
Vues:
26
>For a given order of rows in a SQL Server table ( I.e. the natural order or the order an ORDER BY clause would generate ), is it possible to efficiently query the Nth row in the table?

Bill,

There's no such thing as "the natural order" in the SQL Server. The data in the tables isn't stored in any particular order unless there's clustered index. What are you trying to do?
Anyway, Here's couple of tways to do that. If table has primary key, than it would be faster to get first PK and than join result back to the source table as shown in #2.
USE NorthWind
-- #1
SELECT TOP 1 * FROM (
	SELECT TOP 125 * 
		FROM [Order Details]
		ORDER BY UnitPrice DESC
	) 	dt1	
-- # 2
SELECT * 
	FROM (
		SELECT TOP 1 * 
			FROM (
				SELECT TOP 125 OrderID, ProductID 
					FROM [Order Details]
					ORDER BY UnitPrice DESC
				) 	dt1		
		) dt2
	JOIN [Order Details] odt	
	ON odt.OrderID = dt2.OrderID 
		AND odt.ProductID = dt2.ProductID
	
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform