2017-08-16 37 views
1

是否有可能根据current_date(一个int值)查询一个字段,一个整数数组并返回一个日期数组。Postgres投射由current_date计算的日期数组 - 整数数组

我的数据结构是一样的东西

_Category_ 
name:string 
send_reminders:array[integer] 

_Appointment_ 
category_id:integer 
title:string 
event_date:date 

_Customer_ 
appointemtn_id:integer 
name:string 
contact:boolean 

我想查询谁拥有在该日期约会提醒客户。我以为我可以通过

  1. 使用类别做到这一点:send_reminders生成日期的数组([1,2,3]产生[昨天,前一天,前两天])
  2. 然后用约会日期在该阵列中的条件。
  3. 选择联系方式为true的客户。
+1

你能分享一些样本数据和你想要的结果吗?这会让问题更加清晰,并会帮助我们帮助你。 – Mureinik

回答

2
select * 
from 
    category cat 
    inner join 
    appointment ap on cat.category_id = ap.category_id 
    inner join 
    customer ct on ct.appointment_id = ap.appointment_id 
where 
    ap.event_date in (
     select current_date - i 
     from unnest(cat.send_reminders) u (i) 
    ) 
    and ct.contact 
+0

unnest!谢谢! – Travis