mysql query based on value of previous row -


I want to run a mysql query where the results are directly based on the cell of the interest at the value of the cell. For example: I am calculating the time spent in a series of behaviors and would like to identify such instances where the period of walk time runs longer than the time of feeding immediately. Here is a sample data set:

  ID Duration (minutes) Time Behavior 1 21 9: 1 Spec 1 31 9: 22 Eat 1 15 9:53 Let's 1 21 10:14 Eat 2 7 1:00 Run 2 9 1:07 Eat 2 4 1:16 Walking  

I want to identify two rows of my query, in which the 'duration' amount is small for the previous Line IE: The third entry for ID1, and the third entry for the ID2.

Any help would be greatly appreciated by Thx

Sample data :

  create table T (PKI et auto_interpret primary key, `id` at,` duration` integer, `tod` time,` transaction 'varchar (4)); Price (1, 21, '9: 1', 'Speech'), (1, 31, '9: 22', 'Khao', 'T' (`id`,` Period`, `Todd`, 'Behavior INSERT '), (1, 15,' 9:53 ',' speech '), (1, 21, '10: 14', 'eat'), (2, 7, '01: 00 ' , 'Speech'), (2, 9, '01: 07 ',' Kha '), (2, 4, '01: 16', 'speech');  

Note, I've added another column that is used as the primary key (auto_ignment), because things are less complicated and you should also be the primary key.

query:

  id, duration, selection of TOD, behavior (Pre__dry_longer, @prev_duration: = if (@prev_id! = ID , Blank, duration), @prev_id: Select the TD (* Prev_duration & gt; duration and @prev_id = ID, 1, 0) from id., (@prev_iduration: = duration, select @prev_id: pkai range From the T-Order ID 1) var_init where pkai! = (Id, by TOD) Select from the order minimum (PKE)) where prev_entry_longer = 1;  

Result:

  | ID | DURATION | Todd | BEHAVIOR | | ---- | ---------- | -------------------------------- | ---------- | | 1 | 15 | January, 01 9: 53: 00 + 0000 | Let's go | 2 | 4 | January 01 01 01: 16: 00 + 0000 | Let's go    
  • Read more about user defined variables

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -