Postgresql 如何仅选择其他分组的列上具有最大值的行

问题描述 投票:0回答:1

tmch
包含数千条记录,这里是:

CREATE TABLE IF NOT EXISTS public.tmch
(
    id bigserial NOT NULL,
    year integer,
    week integer,
    my_number integer,
    device_id bigint,
    CONSTRAINT tmch_pkey PRIMARY KEY (id)
)

数据样本

1716446 2024    37  13  2
1716447 2024    37  13  2
1716448 2024    37  0   3
1716449 2024    37  11  4
1716450 2024    37  12  4
1716451 2024    37  0   6
1716452 2024    37  0   6
1716453 2024    37  0   6
1716454 2024    37  1   6
1716455 2024    37  1   6
1716456 2024    37  9   7

这是一个查询,我在其中计算每个分组的周、年和 device_id 中 my_number 出现的次数

select count(my_number) c, my_number, device_id, year, week from tmch
group by my_number, device_id, year, week
order by device_id asc, c desc

此查询的结果:

6   16  2   2024    37
4   17  2   2024    37
4   15  2   2024    37
4   0   2   2024    37
3   11  2   2024    37
3   14  2   2024    37
2   13  2   2024    37
2   1   2   2024    37
2   18  2   2024    37
2   12  2   2024    37
1   10  2   2024    37
1   2   2   2024    37
8   15  3   2024    37
6   16  3   2024    37
5   14  3   2024    37
4   17  3   2024    37
4   12  3   2024    37
3   7   3   2024    37
3   20  3   2024    37  
3   18  3   2024    37
3   19  3   2024    37
3   4   3   2024    37
3   5   3   2024    37
3   6   3   2024    37
1   21  3   2024    37
1   0   3   2024    37
1   3   3   2024    37
1   8   3   2024    37

如何仅获取包含 my_number 且 my_number 计数最高的行?

从以上行的示例中:

6   16  2   2024    37  // because my_number=16 occurs 6 times for device_id=2 y=2024 w=37
8   15  3   2024    37  // because my_number=15 occurs 8 times for device_id=3 y=2024 w=37

我尝试过

row_numbers() over (partition by ...
但没有成功

sql postgresql select max aggregate
1个回答
0
投票
如果您将查询用作子查询,

DISTINCT ON
会执行您想要的操作:

SELECT DISTINCT ON (device_id, week, year)
       c, my_number, device_id, year, week
FROM (SELECT count(my_number) AS c,
             my_number,
             device_id,
             year,
             week
      FROM tmch
      GROUP BY my_number, device_id, year, week) AS sub
ORDER BY device_id, week, year, c DESC;
© www.soinside.com 2019 - 2024. All rights reserved.