Postgres 查询:Select 来自两个不同行的列上具有最大值的行

Postgres Query: Select the row with maximum value on a column from two distinct rows

 user_id | sum  | app_id | app_count
---------+------+--------+-----------
       1 |  100 |      3 |         1
       2 |  300 |      2 |         1
       4 | 1100 |      1 |         2
       4 | 1100 |      4 |         1

如何编写查询,以便根据 app_count 的排名选择不同的 user_id

这是我想要的结果:

     user_id | sum  | app_id | app_count
    ---------+------+--------+-----------
           1 |  100 |      3 |         1
           2 |  300 |      2 |         1
           4 | 1100 |      1 |         2

在 Postgres 中,您将使用 distinct on:

select distinct on (user_id) t.*
from t
order by user_id, app_count desc;