| event_id | alert_id | alert_timestamp | 價值 |
|---|---|---|---|
| 1 | X | 2022-10-10 | -2 |
| 1 | X | 2022-10-10 | 4 |
| 1 | X | 2022-10-10 | 5 |
| 2 | z | 2022-09-02 | 3 |
我有一個表,其中的行與event_id和alert_id匹配alert_timestamp。我只想保留value在所有其他行匹配的列中具有最小值的單行event_id,alert_id和alert_timestamp。
注意:這只是匹配行的一個示例,該表混合了與給定示例不同的行,匹配onevent_id和alert_idalert_timestamp
此外,如果有單行,即沒有其他行與event_id,alert_id和匹配的行,alert_timestamp則應保持原樣。
uj5u.com熱心網友回復:
這是一個典型的 top-1-per-group 問題。在 Redshift 中,您可以使用row_number()它來解決它:
select event_id, alert_id, alert_timestamp, value
from (
select t.*,
row_number() over(partition by event_id, alert_id, alert_timestamp order by value) rn
from mytable t
) t
where rn = 1
請注意,如果您真的只有 4 列,那么聚合就足夠了:
select event_id, alert_id, alert_timestamp, min(value) as value
from mytable
group by event_id, alert_id, alert_timestamp, value
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529266.html
標籤:sql亚马逊红移每组最大 n
