mysql query based on value of previous row

nmh921

I want to run a mysql query where the results are based on the value of the cell directly above the cell of interest. For example: I am calculating time spent in a series of behaviors and would like to identify instances where the duration of time spent walking is longer than the time spent feeding that occurred immediately prior to walking. Here is a sample data set:

ID   Duration (min)  Time of Day    Behavior
1    21               9:01            Walk
1    31               9:22            Eat
1    15               9:53            Walk
1    21               10:14           Eat
2    7                1:00            Walk
2    9                1:07            Eat
2    4                1:16            Walk

I would like my query to identify the two rows in which the 'Duration' amount is smaller than for the previous row. IE: the third entry for ID1, and the third entry for ID2.

Any help would be greatly appreciated. Thx

fancyPants

Sample data:

CREATE TABLE t
    (pkai int auto_increment primary key, `ID` int, `Duration` int, `TOD` time, `Behavior` varchar(4))
;

INSERT INTO t
    (`ID`, `Duration`, `TOD`, `Behavior`)
VALUES
    (1, 21, '9:01', 'Walk'),
    (1, 31, '9:22', 'Eat'),
    (1, 15, '9:53', 'Walk'),
    (1, 21, '10:14', 'Eat'),
    (2, 7, '01:00', 'Walk'),
    (2, 9, '01:07', 'Eat'),
    (2, 4, '01:16', 'Walk')
;

Note, that I added another column which is used as primary key (auto_increment). It makes things less complicated here and you should have a primary key anyway.

Query:

select ID, Duration, TOD, Behavior from (
    select
    t.*,
    if(@prev_duration > Duration and @prev_id = ID, 1, 0) as prev_entry_longer,
    @prev_duration := if(@prev_id != ID, null, Duration),
    @prev_id := ID
    from
    t
    , (select @prev_duration := Duration, @prev_id := ID from t order by pkai limit 1) var_init
    where pkai != (select min(pkai) from t)
    order by ID, TOD
) sq
where prev_entry_longer = 1;

Result:

| ID | DURATION |                            TOD | BEHAVIOR |
|----|----------|--------------------------------|----------|
|  1 |       15 | January, 01 1970 09:53:00+0000 |     Walk |
|  2 |        4 | January, 01 1970 01:16:00+0000 |     Walk |
  • read more about user defined variables here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calculate value based on value from same column of the previous row in spark

From Java

Fill NaN based on previous value of row

From Dev

Get previous row's value on a MySQL view

From Dev

MySQL CASE based on previous CASE value

From Dev

MySQL select rows based on previous query

From Dev

Update value based on the previous updated value mysql

From Dev

User-variable to select value from previous row fine in MySQL 5.5 but in 5.6 it seems to be running the SELECT part of the query in a different order

From Dev

compute current row col value based on values in current and previous row

From Dev

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

From Dev

How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value?

From Dev

Fill NaN based on previous value of row

From Dev

Add new value to row based on content of previous row

From Dev

MySql increment or not based on a previous value

From Dev

Mysql Query - Get row value based on column name in a different table

From Dev

MySQL CASE based on previous CASE value

From Dev

Query: Check previous row value

From Dev

How to update a MySQL row based on previous row?

From Dev

mysql query based on value of previous row

From Dev

MySql Query Limit based on Sub Query Value

From Dev

Groupby based on value in previous row

From Dev

MySQL select rows based on previous query

From Dev

User-variable to select value from previous row fine in MySQL 5.5 but in 5.6 it seems to be running the SELECT part of the query in a different order

From Dev

MYSQL multiply column value by value in previous row

From Dev

Order SQL Query Depending on Value of Previous Row

From Dev

Select value based on value of another row in mysql

From Dev

sql query for difference between current row and previous row based on datetime

From Dev

Pandas - calculate row value based on previous calculated row value

From Dev

Mysql PHP query - add to previous value

From Dev

selection based on previous row value

Related Related

  1. 1

    Calculate value based on value from same column of the previous row in spark

  2. 2

    Fill NaN based on previous value of row

  3. 3

    Get previous row's value on a MySQL view

  4. 4

    MySQL CASE based on previous CASE value

  5. 5

    MySQL select rows based on previous query

  6. 6

    Update value based on the previous updated value mysql

  7. 7

    User-variable to select value from previous row fine in MySQL 5.5 but in 5.6 it seems to be running the SELECT part of the query in a different order

  8. 8

    compute current row col value based on values in current and previous row

  9. 9

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

  10. 10

    How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value?

  11. 11

    Fill NaN based on previous value of row

  12. 12

    Add new value to row based on content of previous row

  13. 13

    MySql increment or not based on a previous value

  14. 14

    Mysql Query - Get row value based on column name in a different table

  15. 15

    MySQL CASE based on previous CASE value

  16. 16

    Query: Check previous row value

  17. 17

    How to update a MySQL row based on previous row?

  18. 18

    mysql query based on value of previous row

  19. 19

    MySql Query Limit based on Sub Query Value

  20. 20

    Groupby based on value in previous row

  21. 21

    MySQL select rows based on previous query

  22. 22

    User-variable to select value from previous row fine in MySQL 5.5 but in 5.6 it seems to be running the SELECT part of the query in a different order

  23. 23

    MYSQL multiply column value by value in previous row

  24. 24

    Order SQL Query Depending on Value of Previous Row

  25. 25

    Select value based on value of another row in mysql

  26. 26

    sql query for difference between current row and previous row based on datetime

  27. 27

    Pandas - calculate row value based on previous calculated row value

  28. 28

    Mysql PHP query - add to previous value

  29. 29

    selection based on previous row value

HotTag

Archive