Postgres - 找到数组的最小值

问题描述 投票:2回答:5

假设我有一个这样的表:

  link_ids  |  length
------------+-----------
 {1,4}      | {1,2}
 {2,5}      | {0,1}

我怎样才能找到每个link_ids的最小长度?

所以最终输出看起来像:

  link_ids  |  length
------------+-----------
 {1,4}      | 1
 {2,5}      | 0
sql arrays postgresql min unnest
5个回答
6
投票

假设一个这样的表:

CREATE TABLE t (
  link_ids int[] PRIMARY KEY     -- which is odd for a PK
, length int[]
, CHECK (length <> '{}'::int[])  -- rules out null and empty in length
);

此查询适用于Postgres 9.3+

SELECT link_ids, min(len) As min_length
FROM   t, unnest(t.length) len  -- implicit LATERAL join
GROUP  BY 1;

或者你创建一个小功能(Postgres 8.4+):

CREATE OR REPLACE FUNCTION arr_min(anyarray) RETURNS anyelement AS
'SELECT min(i) FROM unnest($1) i' LANGUAGE sql IMMUTABLE;

然后:

SELECT link_ids, arr_min(length) AS min_length FROM t;

或者,为了快速实现这一点,只要我们处理的是integer数组的琐碎长度,你就可以使用额外的模块intarray并使用内置的sort() function(Postgres 8.3+):

SELECT link_ids, (sort(length))[1] AS min_length FROM t;

2
投票

假设表名是tlink_ids的每个值都是唯一的。

select link_ids, min(len)
from (select link_ids, unnest(length) as len from t) as t
group by link_ids;

 link_ids | min
----------+-----
 {2,5}    |   0
 {1,4}    |   1

1
投票

对Erwin的答案的一个小补充 - 有时unnest的子查询甚至比横向连接更便宜。

我使用了Erwin的答案中的表定义并填写了它:

t=# insert into t select '{1}'::int[]||g,'{1}'::int[]||g from generate_series(1,9999,1) g;
INSERT 0 9999
t=# select * from t order by ctid desc limit 1;
 link_ids |  length
----------+----------
 {1,9999} | {1,9999}
(1 row)

然后分析LATERAL JOIN:

t=# explain analyze select link_ids,max(r) from t, unnest(length) r where link_ids = '{1,9999}' group by 1;
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=0.29..4.81 rows=1 width=33) (actual time=0.030..0.030 rows=1 loops=1)
   ->  Nested Loop  (cost=0.29..4.30 rows=100 width=33) (actual time=0.025..0.027 rows=2 loops=1)
         ->  Index Scan using t_pkey on t  (cost=0.29..2.30 rows=1 width=58) (actual time=0.015..0.016 rows=1 loops=1)
               Index Cond: (link_ids = '{1,9999}'::integer[])
         ->  Function Scan on unnest r  (cost=0.00..1.00 rows=100 width=4) (actual time=0.007..0.007 rows=2 loops=1)
 Total runtime: 0.059 ms
(6 rows)

并尝试子查询:

t=# explain analyze select link_ids, (select max(r) from unnest(length) r) from t where link_ids = '{1,9999}';
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Index Scan using t_pkey on t  (cost=0.29..3.56 rows=1 width=58) (actual time=0.030..0.031 rows=1 loops=1)
   Index Cond: (link_ids = '{1,9999}'::integer[])
   SubPlan 1
     ->  Aggregate  (cost=1.25..1.26 rows=1 width=4) (actual time=0.011..0.011 rows=1 loops=1)
           ->  Function Scan on unnest r  (cost=0.00..1.00 rows=100 width=4) (actual time=0.008..0.008 rows=2 loops=1)
 Total runtime: 0.060 ms
(6 rows)

最后确保结果是一样的:

t=# select link_ids, (select max(r) from unnest(length) r) 
from t 
where link_ids = '{1,9999}';
 link_ids | max
----------+------
 {1,9999} | 9999
(1 row)

t=# select link_ids,max(r) 
from t, unnest(length) r 
where link_ids = '{1,9999}' 
group by 1;
 link_ids | max
----------+------
 {1,9999} | 9999
(1 row)

0
投票

(我会假设link_ids可以有双打,因为没有id栏我们会即兴发挥)。

WITH r AS
(SELECT row_number() OVER() as id,
       link_ids,
       length from Table1)
SELECT DISTINCT ON (id) link_ids,
       unnest(length) 
FROM r 
ORDER BY id, length;

fiddle


0
投票

对于数组的最小值:

SELECT max(x) from unnest(array_name) as x;
© www.soinside.com 2019 - 2024. All rights reserved.