Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine position of row in sql result-set?

Tags:

sql

count

i have a sql query:

select id, name from table order by name

result looks like this:

52 arnold 
33 berta 
34 chris 
47 doris
52 emil

for a given id=47 how can i determine the position in the result set? the result should be 4 because:

52 arnold
33 berta
34 chris

are before (47, doris) and id=41 is on the 4th position in the result set.

How to do this in SQL? How in HQL? In a pagination example, do i have to execute 2 statements or is there a solution where i can retrieve exactly that window which contains the row with id=47?

postgreSQL and java

like image 549
Chris Avatar asked Jul 03 '09 13:07

Chris


People also ask

How do I SELECT a position in SQL?

The POSITION() function returns the position of the first occurrence of a substring in a string. If the substring is not found within the original string, this function returns 0. This function performs a case-insensitive search. Note: The LOCATE() function is equal to the POSITION() function.

How do I find a specific row in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I reference a row number in SQL?

Discussion: If you'd like to number each row in a result set, SQL provides the ROW_NUMBER() function. This function is used in a SELECT clause with other columns. After the ROW_NUMBER() clause, we call the OVER() function.

WHERE is the result set in SQL?

sql. ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object.


2 Answers

The previous posts are correct. Use ROW_NUMBER if using Microsoft SQL Server 2005 or greater.

However, your tags do not specify that you're using MSSQL, so here's a solution that should work across most RDBMS implementations. Essentially, use a correlated subquery to determine the count of rows in the same set that are less than the current row, based on the values of the ORDER clause of the outer query. Something like this:

SELECT      T1.id,
            T1.[name],
            (SELECT COUNT(*) 
             FROM table T2 
             WHERE T2.[name] < T1.[name]) + 1 AS rowpos
FROM        table T1
ORDER BY    T1.[name]
like image 82
Bob Mc Avatar answered Oct 23 '22 19:10

Bob Mc


You don't mention which RDBMS you're running, but if you're using SQL Server 2005 or greater, you can employ the ROW_NUMBER() function - like this:

SELECT id, name, ROW_NUMBER() OVER (ORDER BY name) AS RowNumber
FROM   MyTable

Then, to fetch a given row, you can use a derived table or a common table expression - like this:

;WITH NumberedRows
AS
(SELECT id, name, ROW_NUMBER() OVER (ORDER BY name) AS RowNumber
FROM    MyTable)
SELECT RowNumber FROM NumberedRows WHERE id = 47

To sort out "which page" the record is on, you would need to divide the row number by the number of records per page, and round up to the nearest integer.

like image 6
Aaron Alton Avatar answered Oct 23 '22 18:10

Aaron Alton