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