Ruby on rails Postgresql使用原始sql填充缺少的日期

Ruby on rails Postgresql使用原始sql填充缺少的日期,ruby-on-rails,postgresql,date,join,select,Ruby On Rails,Postgresql,Date,Join,Select,我将以下表格简化如下: 订单: 退款: 有人能帮我得到上面想要的结果吗。我看过一些例子,但我无法让它与我的场景一起工作。谢谢 我想这会满足你的要求: select d.dt, o.shipping, s.total_orders, coalesce(s.sales_amount, 0) - coalesce(r.refound_amount, 0) net_sales from generate_series(?::timestamp, ?::timestam

我将以下表格简化如下:

订单:

退款:


有人能帮我得到上面想要的结果吗。我看过一些例子,但我无法让它与我的场景一起工作。谢谢

我想这会满足你的要求:

select 
    d.dt, 
    o.shipping,
    s.total_orders,
    coalesce(s.sales_amount, 0) - coalesce(r.refound_amount, 0) net_sales
from generate_series(?::timestamp, ?::timestamp, interval '1 day') d(dt)
left join lateral (
    select 
        count(distinct order_id) total_orders,
        sum(price * quantity) sales_amount,
        array_agg(order_id) order_ids
    from sales s
    where s.date >= d.dt and s.date < d.dt + interval '1 day'
) s on true
left join lateral (
    select sum(o.shipping) shipping
    from orders o
    where o.id = any(s.order_ids)
) o on true
left join lateral (
    select sum(r.amount) refound_amount
    from refunds r
    where r.order_id = any(s.order_ids)
) r on true
查询首先生成给定时间间隔内的所有日期?表示两个日期参数


然后,我们使用一个横向联接和一个聚合查询来获取该期间内发生的所有销售的信息。另一个后续联接带来与第一个横向联接选择的订单id相对应的发货,另一个联接带来相应的退款。

我得到以下错误:op ANY/ALL array requires行右侧的数组:其中o.id=anys.order_id@tee:请完全按照提供的方式运行查询。我的代码有where o.id=anys.order_id注意我看到的结尾处的s..我对它做了一些修改,因为原来的帖子是这个问题的简化版本。但现在它起作用了!谢谢
<id: 1, order_id: 1, price:10.0, qty:2, date: "2020-06-01T01:16:15-04:00">
<id: 2, order_id: 1, price:9.0, qty: 1, date: "2020-06-01T01:16:15-04:00">
<id: 3, order_id: 2, price:15.0, qty:2, date: "2020-06-01T01:23:53-04:00">
<id: 4, order_id: 3, price:4.0, qty: 1, date: "2020-06-01T20:28:18-04:00">
<id: 5, order_id: 3, price:4.0, qty: 2, date: "2020-06-01T20:31:15-04:00">
<id: 6, order_id: 4, price:29.0, qty:1, date: "2020-06-03T20:16:15-04:00">
<id: 1, order_id: 1, qty:1, amount: 9.0, date: "2020-06-01T01:23:15-04:00">
<id: 2, order_id: 4, qty:1, amount: 29.0, date: "2020-06-04T03:34:53-04:00">
"2020-06-01": {shipping: 6, total_orders: 3, net_sales: 62.0},
"2020-06-02": {shipping: 0, total_orders: 0, net_sales: 0}, --> newly added
"2020-06-03": {shipping: 5, total_orders: 1, net_sales: 29},
"2020-06-04": {shipping: 0, total_orders: 1, net_sales: -29.0},
"2020-06-05": {shipping: 0, total_orders: 0, net_sales: 0} --> newly added.
select 
    d.dt, 
    o.shipping,
    s.total_orders,
    coalesce(s.sales_amount, 0) - coalesce(r.refound_amount, 0) net_sales
from generate_series(?::timestamp, ?::timestamp, interval '1 day') d(dt)
left join lateral (
    select 
        count(distinct order_id) total_orders,
        sum(price * quantity) sales_amount,
        array_agg(order_id) order_ids
    from sales s
    where s.date >= d.dt and s.date < d.dt + interval '1 day'
) s on true
left join lateral (
    select sum(o.shipping) shipping
    from orders o
    where o.id = any(s.order_ids)
) o on true
left join lateral (
    select sum(r.amount) refound_amount
    from refunds r
    where r.order_id = any(s.order_ids)
) r on true