具有多个值的整数数组列

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

本列为整数数组类型:

log_session
-----------------------
 {105683,105694}
 {111833}
 {120285}
 {108592}
 {84659,84663}

我想知道有多少

log_session
只有 1 个值(在本例中为 3),有多少具有超过 1 个值(此处为 2)。

编辑

select 
    count(*) filter(where array_length(log_session, 1) = 1) as cnt_length_1,
    count(*) filter(where array_length(log_session, 1) > 1) as cnt_length_more_than_1
from mytable;

ERROR:  function array_length(integer[]) does not exist
LINE 2:     count(*) filter(where array_length(log_session) = 1) as ...
                                  ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

select version();
                                                               version                                                               
-------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.6.20 on x86_64-pc-linux-gnu (Ubuntu 9.6.20-1.pgdg18.04+1), compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
(1 row)

sql arrays postgresql aggregate-functions
1个回答
1
投票

您可以使用

array_length

select 
    count(*) filter(where array_length(log_session, 1) = 1) as cnt_length_1,
    count(*) filter(where array_length(log_session, 1) > 1) as cnt_length_more_than_1
from mytable
cnt_length_1 cnt_length_more_than_1
3 2

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.