Postgres select all columns but group by one column

前端 未结 2 1847
有刺的猬
有刺的猬 2021-01-31 11:43

I have a simple table with a unit_id oid, time timestamp, diag bytea. The primary key is a combination of both time and unit_id.

The idea behind this query is to get the

2条回答
  •  猫巷女王i
    2021-01-31 12:29

    You can join the grouped select with the original table:

    SELECT d.time, d.diag, d.unit_id
    FROM(
        SELECT unit_id, max(time) as max_time
        FROM diagnostics.unit_diag_history
        GROUP BY unit_id
    ) s JOIN diagnostics.unit_diag_history d
    ON s.unit_id = d.unit_id AND s.max_time = d.time
    

提交回复
热议问题