问题描述
test_name,subject_1,subject_2
它有类似的条目
('A',['a','b','c'],['b','d']),('B',['d','a','b'],'b']),etc
现在,我希望像 set(subject_1)-set(subject_2) 那样执行集合减法。因此,对于 'A',输出将是 ['a','c']。对于'B',['d']。
任何帮助!!我尝试在互联网上搜索,但徒劳无功。
解决方法
在 Athena / Presto 中,集合基于 Array。
WITH example_table AS
(SELECT 'A' AS test_name,ARRAY['a','b','c'] AS subject_1,ARRAY['b','d'] AS subject_2 UNION ALL
SELECT 'B',ARRAY['d','a','b'],'b'] )
SELECT test_name,array_except(
subject_1,array_intersect(subject_1,subject_2)
) AS diff
FROM example_table
- WITH 部分用于为主查询创建临时表。
- Diff 是 array_intersect 和 array_except 的组合