我有一张如下表格:
ID array1 array2
7333 | {615593} | {NULL}
7333 | {2013682} | {NULL}
7333 | {26573,1508291} | {NULL}
7333 | {1457957} | {NULL}
7333 | {NULL} | {1063105}
7333 | {NULL} | {107978,408431}
我希望为每个edge_id查询array1和array2的所有唯一项。例如
ID array1_distinct array2_distinct
7333 5 3
我尝试过类似下面的内容
SELECT
id,
array1_count
FROM (
SELECT id,
COUNT(DISTINCT((CASE WHEN array1 is not null THEN (select unnest(array1)b) END)) as array1_count
FROM mytable
GROUP BY id
) totals
limit 1
我总是得到像&#34这样的错误;用作表达式的子查询返回的行超过#34;和"在上下文中调用的集值函数,它不能接受集合"使用者时尝试计算数组中的元素。
答案 0 :(得分:6)
select id, count(distinct a1) as a1, count(distinct a2) as a2
from (
select id, unnest(array1) as a1, unnest(array2) as a2
from t
) s
group by id