[英]Find duplicated values on array column
I have a table with a array column like this: 我有一个带有数组列的表,如下所示:
my_table
id array
-- -----------
1 {1, 3, 4, 5}
2 {19,2, 4, 9}
3 {23,46, 87, 6}
4 {199,24, 93, 6}
And i want as result what and where is the repeated values, like this: 结果我想要重复的值在哪里,像这样:
value_repeated is_repeated_on
-------------- -----------
4 {1,2}
6 {3,4}
Is it possible? 可能吗? I don't know how to do this. 我不知道该怎么做。 I don't how to start it! 我不怎么开始! I'm lost! 我迷路了!
Use unnest
to convert the array to rows, and then array_agg
to build an array from the id
s 使用unnest
将数组转换为行,然后使用array_agg
从id
构建数组
It should look something like this: 它看起来应该像这样:
SELECT v AS value_repeated,array_agg(id) AS is_repeated_on FROM
(select id,unnest(array) as v from my_table)
GROUP by v HAVING Count(Distinct id) > 1
Note that HAVING Count(Distinct id) > 1
is filtering values that don't appear even once 请注意, HAVING Count(Distinct id) > 1
表示过滤的值即使一次也不会出现
The clean way to call a set-returning function like unnest()
is in a LATERAL
join, available since Postgres 9.3: 调用unnest()
这样的set-returning函数的简单方法是在LATERAL
,自Postgres 9.3起可用:
SELECT value_repeated, array_agg(id) AS is_repeated_on
FROM my_table
, unnest(array_col) value_repeated
GROUP BY value_repeated
HAVING count(*) > 1
ORDER BY value_repeated; -- optional
About LATERAL
: 关于LATERAL
:
There is nothing in your question to rule out shortcut duplicates (the same element more than once in the same array ( like I@MSoP commented ), so it must be count(*)
, not count (DISTINCT id)
. 您的问题中没有什么可以排除快捷方式重复项(同一数组中同一元素不止一次( 如I @ MSoP注释 ),因此它必须是count(*)
,而不是count (DISTINCT id)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.