select observation by group with max date SQL via R

前端 未结 2 510
梦毁少年i
梦毁少年i 2021-01-29 11:44

I have data in a similar structure as shown below. This is in R code but if you can just write the query without the R stuff thats fine too.

I have multiple groups and

2条回答
  •  长情又很酷
    2021-01-29 12:04

    I don't know R but your SQL would be something like:

    SELECT * FROM YourTable as A 
    INNER JOIN (SELECT GROUPS, MAX(DATES) AS MAX_DATE FROM YourTable GROUP BY GROUPS) AS B 
    ON A.GROUPS = B.GROUPS AND B.MAX_DATE = A.DATES
    

    This would identify the max date for each group (derived table B) then match them with the records from the main table (table A).

提交回复
热议问题