Postgres SQL: 如何select postgres 中相同时间戳的"media" 列中的最高值?
Postgres SQL: How to select the highest value in the "media" column for the same time stamp in postgres?
我正在通过 DataGrip 程序使用 possql。我有以下 table:
timestamp | Channel | media
-----------------------------------------
2020-04-29 00:00:00 | 3 | 1.2
2020-04-29 00:00:00 | 4 | 2
2020-04-29 00:00:00 | 5 | 1
2020-04-29 00:10:00 | 3 | 2
2020-04-29 00:10:00 | 4 | 1.5
2020-04-29 00:10:00 | 5 | 3
我想对每个"timestamp"按"media"列中的最大值排序,如下:
timestamp | Channel | media
-----------------------------------------
2020-04-29 00:00:00 | 4 | 2
2020-04-29 00:10:00 | 5 | 3
我该怎么做?
我试过了,但是没用,它在重复原来的 table:
SELECT timestamp, max(media), channel
FROM monitoring_aggregate
GROUP BY timestamp, channel
ORDER BY timestamp
在 Postgres 中,只需使用 distinct on
来解决这个 top-1-per-group 问题:
select distinct on (timestamp) ma.*
from monitoring_aggregate ma
order by timestamp, media desc
我正在通过 DataGrip 程序使用 possql。我有以下 table:
timestamp | Channel | media
-----------------------------------------
2020-04-29 00:00:00 | 3 | 1.2
2020-04-29 00:00:00 | 4 | 2
2020-04-29 00:00:00 | 5 | 1
2020-04-29 00:10:00 | 3 | 2
2020-04-29 00:10:00 | 4 | 1.5
2020-04-29 00:10:00 | 5 | 3
我想对每个"timestamp"按"media"列中的最大值排序,如下:
timestamp | Channel | media
-----------------------------------------
2020-04-29 00:00:00 | 4 | 2
2020-04-29 00:10:00 | 5 | 3
我该怎么做?
我试过了,但是没用,它在重复原来的 table:
SELECT timestamp, max(media), channel
FROM monitoring_aggregate
GROUP BY timestamp, channel
ORDER BY timestamp
在 Postgres 中,只需使用 distinct on
来解决这个 top-1-per-group 问题:
select distinct on (timestamp) ma.*
from monitoring_aggregate ma
order by timestamp, media desc