Update row with value from previous row in a GROUP BY query in Postgres

user1813867

Imagine I have a table like so:

account_id  date      value
1           1/1/2015  5
1           1/3/2015  7
1           1/7/2015  8
3           1/2/2015  4

What if I wanted to do an ORDER BY DATE and GROUP BY account_id and update each row with the value of the row before it?

So the end result should be:

account_id  date      value  prev_value
1           1/1/2015  5     null
1           1/3/2015  7     5
1           1/7/2015  8     7
3           1/2/2015  4     null

Any good way to do that in a single query?

Dmitry Savinkov

lag(value anyelement [, offset integer [, default anyelement ]]) window function will do it for you, which basically:

returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null

WITH t(account_id,date,value) AS ( VALUES
  (1,'1/1/2015'::DATE,5),
  (1,'1/3/2015'::DATE,7),
  (1,'1/7/2015'::DATE,8),
  (3,'1/2/2015'::DATE,4)
)
SELECT
  *,
  lag(value,1) OVER (PARTITION BY account_id) AS prev_value
FROM t
GROUP BY 1,2,3
ORDER BY 1,2,3;

Result:

 account_id |   date   | value | prev_value 
------------+----------+-------+------------
          1 | 1/1/2015 |     5 |           
          1 | 1/3/2015 |     7 |          5
          1 | 1/7/2015 |     8 |          7
          3 | 1/2/2015 |     4 |           
(4 rows)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

subtract value from previous row by group

From Dev

Query: Check previous row value

From Dev

How to get column value from previous row for each group?

From Dev

Influxdb - Subtracting value from previous row, group by time

From Dev

Update Row with Previous Row

From Dev

Postgres Next/Previous row SQL Query

From Dev

Postgres Next/Previous row SQL Query

From Dev

Postgres UPDATE..FROM query with multiple updates on the same row

From Dev

Postgres UPDATE..FROM query with multiple updates on the same row

From Dev

Referencing a value from the previous row

From Dev

Using value from previous row

From Dev

Previous row value group by primary key in SQL

From Dev

R: Update with value of previous row (subject to condition)

From Dev

sqlite: update all rows in group with value from first row

From Dev

mysql query based on value of previous row

From Dev

mysql query based on value of previous row

From Dev

Order SQL Query Depending on Value of Previous Row

From Dev

Update current row using previous row's value in Oracle

From Dev

Using a value from a previous row to calculate a value in the next row

From Dev

get the previous row of a query

From Dev

Get the value from previous row in SSRS

From Dev

Subtract a value from previous row respecting the grouping

From Dev

Getting a Value from a previous Row in DrawColumnCell

From Dev

Roll over value from previous row when row is null

From Dev

SparkSql query to get just previous and next row from cassandra for a defined value

From Dev

How to query a specified row from each group

From Dev

Set multiple column values from previous row using Postgres SQL

From Dev

How to group by looking at previous and next value in each row

From Dev

How to calculate a value based on a column of a previous row in a GROUP BY using SQLite?

Related Related

  1. 1

    subtract value from previous row by group

  2. 2

    Query: Check previous row value

  3. 3

    How to get column value from previous row for each group?

  4. 4

    Influxdb - Subtracting value from previous row, group by time

  5. 5

    Update Row with Previous Row

  6. 6

    Postgres Next/Previous row SQL Query

  7. 7

    Postgres Next/Previous row SQL Query

  8. 8

    Postgres UPDATE..FROM query with multiple updates on the same row

  9. 9

    Postgres UPDATE..FROM query with multiple updates on the same row

  10. 10

    Referencing a value from the previous row

  11. 11

    Using value from previous row

  12. 12

    Previous row value group by primary key in SQL

  13. 13

    R: Update with value of previous row (subject to condition)

  14. 14

    sqlite: update all rows in group with value from first row

  15. 15

    mysql query based on value of previous row

  16. 16

    mysql query based on value of previous row

  17. 17

    Order SQL Query Depending on Value of Previous Row

  18. 18

    Update current row using previous row's value in Oracle

  19. 19

    Using a value from a previous row to calculate a value in the next row

  20. 20

    get the previous row of a query

  21. 21

    Get the value from previous row in SSRS

  22. 22

    Subtract a value from previous row respecting the grouping

  23. 23

    Getting a Value from a previous Row in DrawColumnCell

  24. 24

    Roll over value from previous row when row is null

  25. 25

    SparkSql query to get just previous and next row from cassandra for a defined value

  26. 26

    How to query a specified row from each group

  27. 27

    Set multiple column values from previous row using Postgres SQL

  28. 28

    How to group by looking at previous and next value in each row

  29. 29

    How to calculate a value based on a column of a previous row in a GROUP BY using SQLite?

HotTag

Archive