如何从postgresql中的数组中排除?
我想在下面的查询中更改数组列如何从postgresql中的数组中排除?,sql,arrays,postgresql,Sql,Arrays,Postgresql,我想在下面的查询中更改数组列call\u type的条件,以便它排除所有已“访问”的内容。 如何使用该阵列部件 select staff_id, COUNT(event_id) as offered from call_logs as logs where visit_offered and contact_date between now() - interval '1 weeks' and now() and provider_type = 'Contractor' and conta
call\u type
的条件,以便它排除所有已“访问”的内容。如何使用该阵列部件
select staff_id,
COUNT(event_id) as offered
from call_logs as logs
where visit_offered
and contact_date between now() - interval '1 weeks' and now()
and provider_type = 'Contractor'
and contact_with != 'company_staff'
and direction = 'outbound'
and outcome = 'Successful'
and call_type && array ['Schedule','schedule_visit']
group by staff_id;
要简单地将数组元素作为一个整体进行匹配请使用,否定:
AND NOT call_type @> '{schedule_visit}'::text[]
注意数组包装器。通常不需要转换为text[]
,但可以帮助避免歧义
如果列可以是NULL
,并且您不想排除这些行:
AND (call_type @> '{schedule_visit}'::text[]) IS NOT TRUE
可通过索引支持:
表达式:
AND NOT EXISTS (
SELECT FROM unnest(logs.call_type) call
WHERE call ILIKE '%visit_occurred%' -- case insensitive?
)
对此没有索引支持。JSON数组的相关答案(原理相同):
另一种方法是将数组列规范化为一个单独的n:1表。“排除所有已发生“访问”的内容”有点模糊。是要精确匹配数组元素还是标识包含给定字符串的元素?请随时公开你对Postgres的看法。