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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

MySQL query to return all row with same value only if all of them with equal to diffrent value

来自分类Dev

Change values in row based on a column value and replacing specified column range

来自分类Dev

How to get the previous row value in the result set of type forward only using Java

来自分类Dev

将 mysql 查询转换为 CodeIgniter $query->row

来自分类Dev

MySQL Query For Total Online Time Based On Login/Logout Entries

来自分类Dev

How to select previous row in a QTreeView?

来自分类Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

来自分类Dev

将 CSV 文件导入 MYSQL 时出错:Errno 1136 - ER_WRONG_VALUE_COUNT_ON_ROW

来自分类Dev

Search query optimization in mysql

来自分类Dev

Addition from MySQL query

来自分类Dev

MySQL Query ORDER BY with modulus

来自分类Dev

MySQL Query with count, group by

来自分类Dev

PHP和mysql mysql_fetch_row

来自分类Dev

Copy row based on multiple criteria(s)

来自分类Dev

PHP中的多个$ query = mysql_query

来自分类Dev

从 mysql _query 转换为 mysqli_query

来自分类Dev

Request.Form has no value when previous page is a secure page?

来自分类Dev

MySQL NOT IN Query much slower after Mysql Upgrade

来自分类Dev

MySQL index usage query optimization

来自分类Dev

mysql spring data error(QUERY)

来自分类Dev

mysql: write query with if in where part

来自分类Dev

MYSQL query minus operator not working

来自分类Dev

MYSQL QUERY //发出COUNT个

来自分类Dev

MYSQL QUERY连接多个表

来自分类Dev

MySQL JOIN QUERY与多个条款

来自分类Dev

MySQL Query(带子查询)优化

来自分类Dev

SQL Server: how to limit results based on query

来自分类Dev

Splitting quantities to fulfill orders - Set based query

Related 相关文章

  1. 1

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

  2. 2

    MySQL query to return all row with same value only if all of them with equal to diffrent value

  3. 3

    Change values in row based on a column value and replacing specified column range

  4. 4

    How to get the previous row value in the result set of type forward only using Java

  5. 5

    将 mysql 查询转换为 CodeIgniter $query->row

  6. 6

    MySQL Query For Total Online Time Based On Login/Logout Entries

  7. 7

    How to select previous row in a QTreeView?

  8. 8

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  9. 9

    将 CSV 文件导入 MYSQL 时出错:Errno 1136 - ER_WRONG_VALUE_COUNT_ON_ROW

  10. 10

    Search query optimization in mysql

  11. 11

    Addition from MySQL query

  12. 12

    MySQL Query ORDER BY with modulus

  13. 13

    MySQL Query with count, group by

  14. 14

    PHP和mysql mysql_fetch_row

  15. 15

    Copy row based on multiple criteria(s)

  16. 16

    PHP中的多个$ query = mysql_query

  17. 17

    从 mysql _query 转换为 mysqli_query

  18. 18

    Request.Form has no value when previous page is a secure page?

  19. 19

    MySQL NOT IN Query much slower after Mysql Upgrade

  20. 20

    MySQL index usage query optimization

  21. 21

    mysql spring data error(QUERY)

  22. 22

    mysql: write query with if in where part

  23. 23

    MYSQL query minus operator not working

  24. 24

    MYSQL QUERY //发出COUNT个

  25. 25

    MYSQL QUERY连接多个表

  26. 26

    MySQL JOIN QUERY与多个条款

  27. 27

    MySQL Query(带子查询)优化

  28. 28

    SQL Server: how to limit results based on query

  29. 29

    Splitting quantities to fulfill orders - Set based query

热门标签

归档