PostgreSQL与SUM 2/4表的左连接
我试图用4个表中的2个表进行左连接,但我被卡住了 在下面底部的预期结果中,有一个名为begin_stock和end_stockresult of daily_stock.qty的新字段,该字段按日期_stat进行过滤,但不需要使用总和,因为这些值是最终值 您可以在这里尝试: 以下是表格: 产品: 用法:需要使用sumPostgreSQL与SUM 2/4表的左连接,sql,postgresql,pivot,aggregate-functions,lateral-join,Sql,Postgresql,Pivot,Aggregate Functions,Lateral Join,我试图用4个表中的2个表进行左连接,但我被卡住了 在下面底部的预期结果中,有一个名为begin_stock和end_stockresult of daily_stock.qty的新字段,该字段按日期_stat进行过滤,但不需要使用总和,因为这些值是最终值 您可以在这里尝试: 以下是表格: 产品: 用法:需要使用sum id product_id used date_out 1 1 10 2020-12-18 2 1 20
id product_id used date_out
1 1 10 2020-12-18
2 1 20 2020-12-19
3 2 20 2020-12-18
id product_id restock_amount date_in
1 1 10 2020-12-18
2 1 20 2020-12-19
3 4 10 2020-12-19
产品重新进货:需要使用sum
id product_id used date_out
1 1 10 2020-12-18
2 1 20 2020-12-19
3 2 20 2020-12-18
id product_id restock_amount date_in
1 1 10 2020-12-18
2 1 20 2020-12-19
3 4 10 2020-12-19
每日库存
2018年12月2020日至2019年12月2020日的预期结果:
可以使用聚合和横向联接:
select p.*, ds.*, pr.*, u.*
from product p
cross join lateral (
select
max(case when ds.date_stat = date '2020-12-18' then qty end) as begin_stock,
max(case when ds.date_stat = date '2020-12-19' then qty end) as end_stock
from daily_stock ds
where
ds.product_id = p.id
and ds.date_stat in (date '2020-12-18', date '2020-12-19')
) ds
cross join lateral (
select coalesce(sum(u.used), 0) as used
from usage u
where
u.product_id = p.id
and u.date_out >= date '2020-12-18'
and u.date_out <= date '2020-12-19'
) u
cross join lateral (
select coalesce(sum(pr.restock_amount), 0) as restock
from product_restock pr
where
pr.product_id = p.id
and pr.date_in >= date '2020-12-18'
and pr.date_in <= date '2020-12-19'
) pr
:
您能显示您迄今为止尝试过的查询吗?请在加入之前聚合每个表。。。从p LEFT JOIN SELECT id,SUMx AS x FROM y GROUP BY id AS z ON p.id=z.idhello,感谢您的回答,但仍然发现了一个错误:@Diand:I修复了查询-并测试了id.hmm,是否有办法将结束库存放在表的末尾?看起来begin_stock和end_stock是不能分开的。@Diand:这只是对外部select子句的一个小改动:select p.*,ds.begin_stock,pr.*,u.*,ds.end_stock from…嗯,无论如何,谢谢你,伙计!祝您有个美好的一天!您可以使用windws函数。因此,您可以获得begin\u stock和end\u stock
select p.*, ds.*, pr.*, u.*
from product p
cross join lateral (
select
max(case when ds.date_stat = date '2020-12-18' then qty end) as begin_stock,
max(case when ds.date_stat = date '2020-12-19' then qty end) as end_stock
from daily_stock ds
where
ds.product_id = p.id
and ds.date_stat in (date '2020-12-18', date '2020-12-19')
) ds
cross join lateral (
select coalesce(sum(u.used), 0) as used
from usage u
where
u.product_id = p.id
and u.date_out >= date '2020-12-18'
and u.date_out <= date '2020-12-19'
) u
cross join lateral (
select coalesce(sum(pr.restock_amount), 0) as restock
from product_restock pr
where
pr.product_id = p.id
and pr.date_in >= date '2020-12-18'
and pr.date_in <= date '2020-12-19'
) pr
id | product_name | begin_stock | end_stock | restock | used
-: | :----------- | ----------: | --------: | ------: | ---:
1 | abc | 10 | 10 | 30 | 30
2 | aaa | 10 | -10 | 0 | 20
3 | bbb | 10 | 10 | 0 | 0
4 | ddd | 10 | 20 | 10 | 0
with start_end as
(
select ds.product_id,ds.date_stat,ds.qty
,row_number() over(partition by ds.product_id order by ds.date_stat asc) start_dt
,row_number() over(partition by ds.product_id order by ds.date_stat desc) end_dt
from daily_stock ds
)
,sum_of_restock_amount as
(
select
pr.product_id ,sum(pr.restock_amount) restock
from product_restock pr
group by pr.product_id
)
,sum_of_usage as
(
select
u.product_id ,sum(u.used) used
from usage u
group by u.product_id
)
select pn.*,st.qty,res.restock,us.used,en.qty
from product pn
left join start_end st
on pn.id = st.product_id
and st.start_dt = 1
and st.date_stat between '2020-12-18' and '2020-12-19'
left join sum_of_restock_amount res
on pn.id = res.product_id
left join sum_of_usage us
on pn.id = us.product_id
left join start_end en
on pn.id = en.product_id
and en.end_dt = 1
and en.date_stat between '2020-12-18' and '2020-12-19'