How to select id with max date group by category in PostgreSQL?

前端 未结 4 1419
死守一世寂寞
死守一世寂寞 2020-11-27 10:54

For an example, I would like to select id with max date group by category, the result is: 7, 2, 6

id  category  date
1   a         2013-01-01
2   b         2         


        
4条回答
  •  猫巷女王i
    2020-11-27 11:40

    Try this one:

    SELECT t1.* FROM Table1 t1
    JOIN 
    (
       SELECT category, MAX(date) AS MAXDATE
       FROM Table1
       GROUP BY category
    ) t2
    ON T1.category = t2.category
    AND t1.date = t2.MAXDATE
    

    See this SQLFiddle

提交回复
热议问题