0
我想選擇表中列中某些數目的列滿足比較條件(> = n)的行。這可能沒有使用unnest?用PostgreSQL中的數組項的條件查詢
我想選擇表中列中某些數目的列滿足比較條件(> = n)的行。這可能沒有使用unnest?用PostgreSQL中的數組項的條件查詢
unnest()
是計算數組中過濾元素的自然方法。 但是,你可以隱藏在這樣的sql函數中:
create or replace function number_of_elements(arr int[], val int)
returns bigint language sql
as $$
select count(*)
from unnest(arr) e
where e > val;
$$;
with test(id, arr) as (
values
(1, array[1,2,3,4]),
(2, array[3,4,5,6]))
select id, arr, number_of_elements(arr, 3)
from test;
id | arr | number_of_elements
----+-----------+--------------------
1 | {1,2,3,4} | 1
2 | {3,4,5,6} | 3
(2 rows)