GROUP BY having MAX date

后端 未结 4 996
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 19:01

I have problem when executing this code:

SELECT * FROM tblpm n 
WHERE date_updated=(SELECT MAX(date_updated) 
FROM tblpm GROUP BY control_number 
HAVING cont         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-04 19:34

    Fast and easy with HAVING:

    SELECT * FROM tblpm n 
    FROM tblpm GROUP BY control_number 
    HAVING date_updated=MAX(date_updated);
    

    In the context of HAVING, MAX finds the max of each group. Only the latest entry in each group will satisfy date_updated=max(date_updated). If there's a tie for latest within a group, both will pass the HAVING filter, but GROUP BY means that only one will appear in the returned table.

提交回复
热议问题