Question

I would like to have an easy way to run queries like the following query on my time series data:

"What are the most typical events to take place within seven days of eachother"?

I could do this by utilizing SQL and a Java program, by looking at each row and run a query, which looks up all events seven days earlier or later, but this is not very elegant and performance will be horrible. Is there another way to do this elegantly using SQL or a different query language?

The simplified table structure is like the following:

thetime datetime
eventid int
magnitude double
intensity double

I specifically do not want to describe what the data is about, as I am looking for a general solution.

Was it helpful?

Solution

You can use a self join to do something like this. This is valid syntax for SQL server, but it's hard to customize for your setup when we don't know table structure or what RDBMS you are using...

SELECT  TOP 10
        a.Event as 'First Event', 
        b.event as 'Second Event',
        ABS(CAST((a.timefield - b.timefield) as INT)) as 'Time Apart'
FROM MYtable a
INNER JOIN Mytable B
    ON a.IDField <> b.IDField
WHERE (ABS(CAST((a.timefield - b.timefield) as INT)) <= 5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top