I have the following table:
----------------------------------------------
| ID | interestingData | timestamp |
----------------------------------------------
| 1 | 400 | 2016-01-23 17:01:00 |
----------------------------------------------
| 1 | 400 | 2016-01-24 17:01:00 |
----------------------------------------------
| 1 | 350 | 2016-01-25 17:01:00 |
----------------------------------------------
| 2 | 23 | 2016-01-23 17:01:00 |
----------------------------------------------
| 2 | 34 | 2016-01-24 17:01:00 |
----------------------------------------------
| 2 | 12 | 2016-01-25 17:01:00 |
----------------------------------------------
Where our PK is (ID, timestamp)
. I'm attempting to determine a query that will give me the unique IDs and the latest interestingData for which interestingData exceeds a threshold. That would, of course, be done with:
SELECT DISTINCT ID
FROM table
WHERE interestingData > threshold
ORDER BY timestamp DESC;
However, I want the count of every occurrence where interestingData exceeded the threshold. My results table would ideally look like
------------------------------------------------------
| ID | interestingData | timestamp | count |
------------------------------------------------------
| 1 | 350 | 2016-01-25 17:01:00 | 3 |
------------------------------------------------------
Were my threshold 300. I am aware that if you want to pair something distinct with a set of data then a left outer join is going to be in order, but I'm not entirely sure how to go about it. This is the closest I can think of so far.
SELECT DISTINCT ID
FROM table t1
LEFT OUTER JOIN table t2 ON t1.ID = t2.table.ID
WHERE interestingData > 300
ORDER BY timestamp DESC
This gets me the distinct IDs and pairs them with the rest of the data as I need, but no provisions for getting the other parts of the results, let alone the count.
interestingData
andtimestamp
should come from the same (latest) row, yes? Not just be random aggregates like MIN or MAX? Also, if there is a 4th row for ID = 1 but now interestingData = 50, and a 4th row for ID = 2 but now interestingData = 500, what should the result be? Think about all the edge cases you might encounter after being provided a solution before asking for a solution (if you wait until after, it just means more chances that the solution has to change).