在esqueleto中重新创建generate_series函数

时间:2018-07-17 22:06:10

标签: haskell esqueleto

我正试图在此问题的后面写一个esqueleto查询:Aggregating date data by calendar month-答案是以下SQL查询:

select gs.mon, sum(created) as created, sum(deactivated) as deactivated
from generate_series('2018-01-01'::date, '2018-07-01'::date, interval '1 month') gs(mon) left join
   ( ( select created as dte, 1 as created, 0 as deactivated 
       from accounts
     ) union all
     ( select deactivated, 0 as created, 1 as deactivated 
       from accounts
    ) ) a
  on date_trunc('month', a.dte) = gs.mon
group by gs.mon;

但是我不确定如何使用esqueleto库重新创建它。我尝试过使用内部unsafeSqlFunction来触发generate_series的方法,但是无法弄清楚如何使用haskell类型将日期传递给该函数(我可以在SQL,但我不想这样做),因此我走了在haskell中生成日期并将其传递到查询中的路线,就像这样:

dateRange =
  takeWhile (\(_, year) -> year >= 2018) <$> calendarRange

其中calendarRangeIO操作,它返回从当前月份到过去的年/月对的无穷列表。

我现在正在努力找出如何传递这些日期,以便以类似于SQL的方式使用它们,尽管我不确定是否有可能:

select (from . query =<< dateRange)
where
  query dates acc = do
    -- not sure I can put anything here to get esqueleto to do what I want...

0 个答案:

没有答案