作为测试,我创建了这个模式:
CREATE TABLE simple_table (client_id int4, order_id int4);
INSERT INTO simple_table (client_id, order_id)
VALUES
(1,2),(1,3),(1,4),(1,6),(1,8),(1,12),(1,16),(1,18),(1,25),(1,32),(1,33),(1,37),(1,43),
(1,56),(1,57),(1,66),(2,2),(2,3),(2,5),(2,7),(2,9),(2,12),(2,17),(2,19),(2,22),(2,30),
(2,33),(2,38),(2,44),(2,56),(2,58),(2,66)
;
然后使用array_agg:
SELECT client_id, array_agg(order_id) FROM simple_table GROUP BY client_id;
为客户端 1 和客户端 2 创建数组:
| CLIENT_ID | ARRAY_AGG |
----------------------------------------------------------
| 1 | 2,3,4,6,8,12,16,18,25,32,33,37,43,56,57,66 |
| 2 | 2,3,5,7,9,12,17,19,22,30,33,38,44,56,58,66 |
现在我想比较这两行并确定相似的值。从 Postgresql 文档中尝试过&& overlap (have elements in common) ARRAY[1,4,3] && ARRAY[2,1]
,但我遇到了问题。
也许我看错了。任何帮助或指导将不胜感激!