[英]How to exclude or remove NULL values from array and array_agg with 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.