繁体   English   中英

如何从Postgresql中的数组中排除?

[英]How do I exclude from array in postgresql?

我想在下面的查询中更改数组列call_type上的条件,以便它排除具有“ visit_occurred”的所有内容。
我该如何处理该阵列零件?

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;

要简单地将数组元素整体匹配,请使用ARRAY包含运算符@> ,取反:

AND  NOT call_type @> '{schedule_visit}'::text[]

注意数组包装器。 通常不需要转换为text[] ,但可以帮助避免歧义。

如果该列可以为NULL ,并且您不想排除此类行:

AND  (call_type @> '{schedule_visit}'::text[]) IS NOT TRUE

可以由索引支持:

模式匹配更加复杂。 使用一个NOT EXISTS表达式:

AND NOT EXISTS (
   SELECT FROM unnest(logs.call_type) call
   WHERE  call ILIKE '%visit_occurred%'  -- case insensitive?
   )

没有索引支持。 JSON数组的相关答案(相同原理):

一种替代方法是将数组列标准化为单独的n:1表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM