I have a dataset with:
Id. Code. Update time
222. A. 1/1/16
222 B. 1/1/17
123 c 1/1/16
123 d. 1/1/17
I want the data set to filter to:
222 B. 1/1/17
123 d. 1/1/17
How do I do the query for this?
I have a dataset with:
Id. Code. Update time
222. A. 1/1/16
222 B. 1/1/17
123 c 1/1/16
123 d. 1/1/17
I want the data set to filter to:
222 B. 1/1/17
123 d. 1/1/17
How do I do the query for this?
First write a sub-query that finds the latest date for each ID. Then join the sub-query back to the main table to grab the other column(s):
SELECT t.ID, t.code, t.updatetime
FROM table t
INNER JOIN
(SELECT ID, MAX(updatetime) AS mxtime
FROM table
GROUP BY ID) sub
ON t.ID = sub.ID
AND t.updatetime = sub.mxtime
As written, this is a simple aggregation query:
select id, max(code) as code, max(updatetime) as updatetime
from t
group by id;
If this is not what you want, then you need to be clearer about the logic you want to implement.