如何从SQL连接计算可用数量

发布于 2025-01-25 16:59:31 字数 1882 浏览 2 评论 0原文

与加入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 columns cntr_id as contractor, matid as material, qty_given

  • the orderitems table has matid, qty_used, orderid

  • the orders table has cntr_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

result of above query

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:

result of above query

Please help me find the approriate query to get available quantity with contractors.

I am extremely confused

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2025-02-01 16:59:31

感谢您的支持,顺便说一句,我弄清楚了获取所需结果的方法。

select 
  a.matid, 
  a.cntr_id, 
  a.qty_given, 
  coalesce(sum(i.untsreq_perorder), 0) used, 
  coalesce((a.qty_given - i.untsreq_perorder),0) as available 
from 
  allocate a 
  left join orders o on o.cntr_id = a.cntr_id 
  left join material m on m.matid = a.matid 
  left join orderitems i on i.orderid = o.orderid 
  and i.matid = m.matid 
group by 
  a.matid, 
  a.cntr_id, 
  a.qty_given, 
  i.untsreq_perorder 
order by 
  a.cntr_id desc

挽救了我生命的主要逻辑是使用

left join orderitems i on i.orderid = o.orderid 
      and i.matid = m.matid 

您可以在

。 //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.

select 
  a.matid, 
  a.cntr_id, 
  a.qty_given, 
  coalesce(sum(i.untsreq_perorder), 0) used, 
  coalesce((a.qty_given - i.untsreq_perorder),0) as available 
from 
  allocate a 
  left join orders o on o.cntr_id = a.cntr_id 
  left join material m on m.matid = a.matid 
  left join orderitems i on i.orderid = o.orderid 
  and i.matid = m.matid 
group by 
  a.matid, 
  a.cntr_id, 
  a.qty_given, 
  i.untsreq_perorder 
order by 
  a.cntr_id desc

the main logic which saved my life was the use of

left join orderitems i on i.orderid = o.orderid 
      and i.matid = m.matid 

you can have a full view on

https://www.db-fiddle.com/f/vsCsRLEDr6Vu582xwwv3f4/1

thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文