Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

Mysql Show records where timestamp interval is lower then 4 minutes

let's assume i have follow table


| ID | teamid | timestamp |

| 5 | 1 | 2013-07-27 10:19:00 |

| 6 | 2 | 2013-07-27 10:20:00 |

| 7 | 1 | 2013-07-27 10:25:00 |

| 8 | 3 | 2013-07-27 10:26:00 |

| 9 | 1 | 2013-07-27 10:28:00 |

| 10 | 2 | 2013-07-27 10:29:00 |

| 11 | 3 | 2013-07-27 10:30:00 |

| 13 | 3 | 2013-07-27 10:31:00 |

What i need is the records where the interval between the timestamp is lower then 4 minutes and grouped by the team id

so output need looks like

| 7 | 1 | 2013-07-27 10:25:00 |

| 9 | 1 | 2013-07-27 10:28:00 |

| 11 | 3 | 2013-07-27 10:30:00 |

| 13 | 3 | 2013-07-27 10:31:00 |

can someone show me the correct way to solve

tnx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following sql statement will return your desired list:

SELECT table1.id, table1.teamid, table1.timestamp FROM exampleTable table1, exampleTable table2 where table1.id != table2.id AND table1.teamid = table2.teamid AND ABS(table1.timestamp - table2.timestamp) < 400 ORDER BY teamid, id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...