sql - MySQL GROUP BY DateTime +/- 3 seconds -


suppose have table 3 columns:

  • id (pk, int)
  • timestamp (datetime)
  • title (text)

i have following records:

1, 2010-01-01 15:00:00, title 2, 2010-01-01 15:00:02, title 3, 2010-01-02 15:00:00, title 

i need group records within 3 seconds of each other. table, rows 1 , 2 grouped together.

there similar question here: mysql datetime group 15 mins

i found this: http://www.artfulsoftware.com/infotree/queries.php#106

i don't know how convert these methods work seconds. trouble method on question seems me work records falling within bin of time starts @ known point. instance, if floor() work seconds, @ interval of 5 seconds, time of 15:00:04 grouped 15:00:01, not grouped 15:00:06.

does make sense? please let me know if further clarification needed.

edit: set of numbers, {1, 2, 3, 4, 5, 6, 7, 50, 51, 60}, seems might best group them {1, 2, 3, 4, 5, 6, 7}, {50, 51}, {60}, each grouping row depends on if row within 3 seconds of previous. know changes things bit, i'm sorry being wishywashy on this.

i trying fuzzy-match logs different servers. server #1 may log item, "item #1", , server #2 log same item, "item #1", within few seconds of server #1. need aggregate functions on both log lines. unfortunately, have title go on, due nature of server software.

i'm using tom h.'s excellent idea doing little differently here:

instead of finding rows beginnings of chains, can find times beginnings of chains, go , ifnd rows match times.

query #1 here should tell times beginnings of chains finding times not have times below them within 3 seconds:

select distinct timestamp table left join table b on (b.timestamp >= a.timestamp - interval 3 seconds     , b.timestamp < a.timestamp) b.timestamp null 

and each row, can find largest chain-starting timestamp less our timestamp query #2:

select table.id, max(startofchains.timestamp) chainstarttime table join ([query #1]) startofchains on table.timestamp >= startofchains.timestamp group table.id 

once have that, can group wanted.

select count(*) --or whatever table join ([query #2]) groupingquery on table.id = groupingquery.id group groupingquery.chainstarttime 

i'm not entirely sure distinct enough tom h's answer posted separately, sounded having trouble implementation, , thinking it, thought i'd post again. luck!


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -