我有一个Postgres(版本11)表,其中有两个逗号分隔的值,如下所示。 可以进行比较以确定缺少哪个特定值。
Category Comparison category
2219,2220,2991,2992,3577,3617,3624,3884 2992,3617,3884
2145,2150,3594,3597,3600,3626 2150,3594,3600,3626
2237 2237
2991,2992,3884 2991,2992,3884
2991,3884 2991,3884
2145,2993,3597,3631 2993,3631
1113,2882,3490,4,4034,922,985 2882,3490,4,4034,922,985
预期输出:
Category Comparison category Comments
2219,2220,2991,2992.. 2992,3617,3884 2219,2220,..not in category,But all Categories in comparison column is present in Category column. column
2145,2150,3594,3597,3600,3626 2150,3594,3600,3626. 2145,3597 not in category column,But all Categories in comparison column is present in Category column.
2237 2237 Matching
2991,2992,3884 2991,2992,3884 Matching
2991,3884 2991,3884 Matching
2145,2993,3597,3631 2993,3631 2145,3597 not in Category column,But all Categories in comparison column is present in Category column.
答案 0 :(得分:3)
如果您可以安装intarray扩展名,则可以执行以下操作:
select category, comparison_category,
string_to_array(category, ',')::int[] - string_to_array(comparison_category, ',')::int[]
from the_table;
如果您无法安装扩展程序,则它会更加复杂:
select category, comparison_category,
(select string_agg(c,',')
from unnest(string_to_array(category, ',')) as x(c)
where x.c not in (select cc
from unnest(string_to_array(comparison_category, ',')) as t(cc))
)
from the_table;
但是正确的解决方案是修复数据模型。