I need to count all distinct values from table LDS where status = 'ok' AND date >= '2012-01-01' AND if there are multiple identical IDs then only check the one with the newest date: if this id has a status of “ok” then count it.
Table LDS:
ID | STATUS | DATE 1 | ok | 2012-01-01 2 | bad | 2012-01-01 1 | bad | 2012-02-02 3 | ok | 2012-01-01 4 | ok | 1999-01-01
Result should be “1” (ID 3)
I’m assuming that since you want to count distinct IDs, that it doesn’t matter which ID is counted in the case of duplicates, since this doesn’t affect the final count:
EDIT: Updated query to exclude any
IDwhich has a subsequent'bad'status: