Hi there,
I'm grouping some records by their proximity of time. What I do right now (timestamps in unixtime),
First off I do a sub select to grab records that are of interest of me,
(SELECT timestamp AS target_time FROM table WHERE something = cool) AS subselect
Then I want to look at the records that are close in time to those,
SELECT id FROM table, subselect WHERE ABS(target_time - timestamp) < 1800
But here is where I hit my problem. I want to only want the records where the time diffrance between the records around the target_time
is > 20 mins. So to do this, I group by the target_time
and add a HAVING
section.
SELECT id FROM table, first WHERE ABS(target_time - timestamp) < 3600
GROUP BY target_time HAVING MAX(timestamp) - MIN(timestamp) > 1200
This is great, and all the records I don't like are gone, but now I only have the first id
of the group, when I really want all of the ids
. I can use GROUP_CONCAT
but that gives me a be mess I can't do anymore queries on. What I really would like it to get all of the ids
returned from all of these groups that are created. Do I need another SELECT
statement? Or is there just a better way to structure what I got?
Thank you,
A SQL nub.