Question

I have a system that reports the XYZ location of items in a field to a SQL database. I am attempting to filter for false positives (only identifying moving items) by filtering for a known point in time when items pass a point, and at what location the items should be in after passing a point.

My logic is if an item is in one location and time AND in another location and time, it must have moved.

so I have this query:

SELECT tag_ID, X_location*3.28, Y_location*3.28, locate_time
FROM tag_blink_history
WHERE 
     (LOCATE_TIME > '2013-01-29 11:05:51' 
     AND LOCATE_TIME < '2013-01-29 11:06:56' 
     AND ((y_location*3.28 > 61) 
     AND (y_location*3.28 < 67.5)) 
     AND ((x_location*3.28 > 14.5) 
     AND (x_location*3.28 < 17.5)))
     AND (((y_location*3.28 > 70) 
     AND (y_location*3.28 < 75)) 
     AND locate_time < '2013-01-29 11:06:50' )
ORDER BY tag_id DESC

Any ideas? I realize what I am asking SQL with the above query is for something in two places at once (which cannot happen), but what I want are records which exist in both these spatial constraints - I want records for when they were in both, not to ask for a record that is in both at the same time.

Was it helpful?

Solution

I think you need to join on itself -- your current query will never produce any results.

Try something like this to get the tags that have multiple occurences in your WHERE Critera -- however, are you missing some criteria on your 2nd location? This example uses your above example:

SELECT DISTINCT t.tag_ID
FROM tag_blink_history t
   JOIN tag_blink_history t2 ON t.tag_ID = t2.tag_ID AND (t.x_location <> t2.x_location OR t.y_location <> t2.y_location)
WHERE (t.LOCATE_TIME > '2013-01-29 11:05:51' 
      AND t.LOCATE_TIME < '2013-01-29 11:06:56' 
      AND ((t.y_location*3.28 > 61) AND (t.y_location*3.28 < 67.5)) 
      AND ((t.x_location*3.28 > 14.5) AND (t.x_location*3.28 < 17.5)))
      AND (((t2.y_location*3.28 > 70) AND (t2.y_location*3.28 < 75)) 
      AND t2.locate_time < '2013-01-29 11:06:50' )
ORDER BY t.tag_id DESC

Good luck.

OTHER TIPS

You can use aggregation (one of several approaches):

SELECT tag_ID, min(X_location*3.28), max(X_location*3.28), min(Y_location*3.28), max(Y_location*3.28), min(locate_time), max(locate_time)
FROM tag_blink_history
WHERE 
     (LOCATE_TIME > '2013-01-29 11:05:51' 
     AND LOCATE_TIME < '2013-01-29 11:06:56' 
     AND ((y_location*3.28 > 61) 
     AND (y_location*3.28 < 67.5)) 
     AND ((x_location*3.28 > 14.5) 
     AND (x_location*3.28 < 17.5)))
     AND (((y_location*3.28 > 70) 
     AND (y_location*3.28 < 75)) 
     AND locate_time < '2013-01-29 11:06:50' )
GROUP BY
   tag_ID
HAVING
   (min(X_location) <> max(X_location))
   OR
   (min(Y_location) <> max(Y_location))
ORDER BY tag_id DESC

@sgeddes self-join approach above is also good; you could also look into derived tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top