我编写了SQL
select array_agg(id) as ids
from fff
group by a_c_id, a_c_dttm, a_c_code
having COUNT(*)>1
它返回结果
{5,9}
{8,12,13}
{6,10}
{7,11}
如何排除每一行的最大值才能得到这个结果?
{5}
{8,12}
{6}
{7}
最终目标是获得以下结果:{5,8,12,6,7}
我编写了SQL
select array_agg(id) as ids
from fff
group by a_c_id, a_c_dttm, a_c_code
having COUNT(*)>1
它返回结果
{5,9}
{8,12,13}
{6,10}
{7,11}
如何排除每一行的最大值才能得到这个结果?
{5}
{8,12}
{6}
{7}
最终目标是获得以下结果:{5,8,12,6,7}
将除最大分组外的所有ids
个分组汇总到一个表中:
select array_agg(id) as ids
from my_table
where id not in (
select max(id)
from my_table
group by other_col
);
请注意,单元素组也被排除在外,因此您不必单独处理它们.
在db<>fiddle.分钟内进行测试