Skip to main content
denoise, tag
Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620

PostgreSQL - Order a row before/after another based on value in the previous row

I've been trying to figure out a way to order a row after another based on the value of a column from the previous row. In my SQL query below, I first initially want to order the dataset by distance in ascending order. However

However, after that, I'd like to then check (on each row), whether the difference between the "distance" value of the previous row and the "distance" value of the current row is less than 50. If so, I'd then like to sort whichever row has highest "rating" value first.

Any help would be appreciated. Thanks

PostgreSQL - Order a row before/after another based on value in the previous row

I've been trying to figure out a way to order a row after another based on the value of a column from the previous row. In my SQL query below, I first initially want to order the dataset by distance in ascending order. However, after that, I'd like to then check (on each row), whether the difference between the "distance" value of the previous row and the "distance" value of the current row is less than 50. If so, I'd then like to sort whichever row has highest "rating" value first.

Any help would be appreciated. Thanks

Order a row before/after another based on value in the previous row

I've been trying to figure out a way to order a row after another based on the value of a column from the previous row. In my SQL query below, I first initially want to order the dataset by distance in ascending order.

However, after that, I'd like to then check (on each row), whether the difference between the "distance" value of the previous row and the "distance" value of the current row is less than 50. If so, I'd then like to sort whichever row has highest "rating" value first.

edited tags
Link
Bill Karwin
  • 16k
  • 2
  • 31
  • 42
added 63 characters in body
Source Link

pgadmin mytable #1

I've attempted to do this in the SQL query below, however, it seems to not produce the desired result:

enter image description herequery result

I've attempted to do this in the SQL query below, however, it seems to not produce the desired result:

enter image description here

pgadmin mytable #1

I've attempted to do this in the SQL query below, however, it seems to not produce the desired result:

query result

deleted 53 characters in body
Source Link
Loading
edited tags
Link
Loading
added 34 characters in body
Source Link
Loading
Source Link
Loading