如何从SQL连接计算可用数量
与加入Postgres数据库的查询,我很不幸。
我必须找出具有给定情况的可用数量。
我有4个表(分配,订单,订单词,材料):
分配表有3列
cntr_id
作为承包商,matid
as材料,qty_given
OrderItems
表具有Matid,Qty_used,OrderID
order> orders
表具有cntr_id,orderd
我的查询是在每个承包商中找到可用的库存。
我认为加入桌子以获得所需的结果非常容易,但这并不容易。
我尝试了许多连接的组合,但没有收益,因此我需要您的帮助。
让我们看看一个示例 - 我的分配
表看起来像
select cntr_id, matid, qty_given
from allocate
此查询返回所有承包商Matid和Qty_given的列表。
此处的查询返回cntr_id,matid和qty_used:
select
o.cntr_id, i.matid, i.untsreq_perorder used
from
orderitems i
join
orders o on i.orderid = o.orderid
join
material m on m.matid = i.matid
group by
o.cntr_id, i.untsreq_perorder, i.matid
此查询返回 仅在表格表中使用的那些值的列表。通过此查询,我可以通过他们找到所有承包商和qty_used
。
现在,主要问题是如何使用第二个查询来与分配的
表中的承包商获取可用库存。
我使用了几种连接组合,但没有任何成功。
我要加入的
select
a.cntr_id, a.matid, x.used
from
allocate a
left join
(select o.cntr_id, i.matid, i.untsreq_perorder used
from orderitems i
join orders o on i.orderid = o.orderid
join material m on m.matid = i.matid
group by o.cntr_id, i.untsreq_perorder, i.matid) x on a.cntr_id = x.cntr_id
group by
a.cntr_id, a.matid, x.used
结果就是这样:
请帮助我查找适用于承包商数量的资格查询。
我非常困惑
I am out of luck with a query with joins in a Postgres database.
I have to find out the available quantity with the given situation.
I have 4 tables (allocate, orders, orderitems, material):
the
allocate
table has 3 columnscntr_id
as contractor,matid
as material,qty_given
the
orderitems
table hasmatid, qty_used, orderid
the
orders
table hascntr_id, orderid
My query is to find available stock with each contractor.
I thought it would be very easy to join tables to get the desired result but its not that easy.
I have tried many combinations of joins but to no gain and thus I need your help.
Let's see an example - my allocate
table looks like this
select cntr_id, matid, qty_given
from allocate
This query returns a list of all the contractors, matid and qty_given.
The query here returns cntr_id, matid and qty_used:
select
o.cntr_id, i.matid, i.untsreq_perorder used
from
orderitems i
join
orders o on i.orderid = o.orderid
join
material m on m.matid = i.matid
group by
o.cntr_id, i.untsreq_perorder, i.matid
result of above query with joins
This query returns a list of only those values which are used in order table which is fine. With this query I can find all the contractor and qty_used
by them.
Now the main issue is how to use this second query to get available stock with contractors in the allocated
table.
I have used several combinations of joins but without any success.
What I am trying to join is
select
a.cntr_id, a.matid, x.used
from
allocate a
left join
(select o.cntr_id, i.matid, i.untsreq_perorder used
from orderitems i
join orders o on i.orderid = o.orderid
join material m on m.matid = i.matid
group by o.cntr_id, i.untsreq_perorder, i.matid) x on a.cntr_id = x.cntr_id
group by
a.cntr_id, a.matid, x.used
The result is like this:
Please help me find the approriate query to get available quantity with contractors.
I am extremely confused
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的支持,顺便说一句,我弄清楚了获取所需结果的方法。
挽救了我生命的主要逻辑是使用
您可以在
。 //www.db-fiddle.com/f/vscsrledr6vu582xwwv3f4/1
谢谢。
thanks for the support, by the way i have figured out the corret way to fetch the desired result.
the main logic which saved my life was the use of
you can have a full view on
https://www.db-fiddle.com/f/vsCsRLEDr6Vu582xwwv3f4/1
thanks.