Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select adjacent rows to an arbitrary row (in sql or postgresql)?

Tags:

I want to select some rows based on certain criteria, and then take one entry from that set and the 5 rows before it and after it.

Now, I can do this numerically if there is a primary key on the table, (e.g. primary keys that are numerically 5 less than the target row's key and 5 more than the target row's key).

So select the row with the primary key of 7 and the nearby rows:

select primary_key from table where primary_key > (7-5) order by primary_key limit 11;  2 3 4 5 6 -=7=- 8 9 10 11 12 

But if I select only certain rows to begin with, I lose that numeric method of using primary keys (and that was assuming the keys didn't have any gaps in their order anyway), and need another way to get the closest rows before and after a certain targeted row.

The primary key output of such a select might look more random and thus less succeptable to mathematical locating (since some results would be filtered, out, e.g. with a where active=1):

select primary_key from table where primary_key > (34-5)      order by primary_key where active=1 limit 11;  30 -=34=- 80 83 100 113 125 126 127 128 129 

Note how due to the gaps in the primary keys caused by the example where condition (for example becaseu there are many inactive items), I'm no longer getting the closest 5 above and 5 below, instead I'm getting the closest 1 below and the closest 9 above, instead.

like image 922
Kzqai Avatar asked Sep 08 '10 22:09

Kzqai


People also ask

How do I select a row in PostgreSQL?

If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. Second, specify the name of the table from which you want to query data after the FROM keyword.

How do I select rows by rows 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 does select query work in PostgreSQL?

The PostgreSQL SELECT statement retrieves data from a single or several tables in a database, and returns the data in a result table, called a result-set. Use the SELECT statement to return one or more rows matching the specified criteria from the database tables.

How do I select a certain number of rows?

Select one or more rows and columnsSelect the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.


1 Answers

There's a lot of ways to do it if you run two queries with a programming language, but here's one way to do it in one SQL query:

(SELECT * FROM table WHERE id >= 34 AND active = 1 ORDER BY id ASC LIMIT 6) UNION (SELECT * FROM table WHERE id < 34 AND active = 1 ORDER BY id DESC LIMIT 5) ORDER BY id ASC 

This would return the 5 rows above, the target row, and 5 rows below.

like image 62
wuputah Avatar answered Oct 16 '22 01:10

wuputah