SQL:添加两个求和字段并将求和值与另一个字段值进行比较

SQL:添加两个求和字段并将求和值与另一个字段值进行比较,sql,database,oracle,sum,Sql,Database,Oracle,Sum,好的,伙计们,我希望是关于这个话题的最后一个问题 我需要做的是对两个不同表中的字段求和,将它们相加,然后检查总数是否大于客户信用额度 我已经把我的问题分解成更小的任务,这样我就可以试着提出整体的解决方案。这些较小的代码对我来说工作正常,但当在第三段代码中组合时,它们的总和不正确。首先,我计算了第一个求和字段,如图所示: --summmed open invoices SELECT company, sum(unit_price * invoice_qty) as open_invoice

好的,伙计们,我希望是关于这个话题的最后一个问题

我需要做的是对两个不同表中的字段求和,将它们相加,然后检查总数是否大于客户信用额度

我已经把我的问题分解成更小的任务,这样我就可以试着提出整体的解决方案。这些较小的代码对我来说工作正常,但当在第三段代码中组合时,它们的总和不正确。首先,我计算了第一个求和字段,如图所示:

 --summmed open invoices

 SELECT company,  sum(unit_price * invoice_qty) as open_invoices

 FROM 
  (SELECT arc.company, ard.unit_price, ard.invoice_qty, arc.credit_limit
   FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc
   WHERE ard.arprepost_id = arp.ID
       AND arp.arcusto_id = arc.ID)

 GROUP BY company
 ORDER BY company;
 --summed open orders

 SELECT company, (sum (unit_price * total_qty_ord)) as total_open_orders

 FROM 
   (SELECT arc.company, od.unit_price, od.total_qty_ord, arc.credit_limit   
    FROM iqms.arcusto arc, iqms.orders o, iqms.ord_detail od
    WHERE od.orders_id = o.ID
       AND o.arcusto_id = arc.ID
       AND (od.cumm_shipped < od.total_qty_ord  OR od.cumm_shipped IS NULL))

 GROUP BY company
 ORDER BY company;
这张表中的数字加起来是正确的,我已经手动检查过了。接下来,我总结了我需要的其他字段,如图所示:

 --summmed open invoices

 SELECT company,  sum(unit_price * invoice_qty) as open_invoices

 FROM 
  (SELECT arc.company, ard.unit_price, ard.invoice_qty, arc.credit_limit
   FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc
   WHERE ard.arprepost_id = arp.ID
       AND arp.arcusto_id = arc.ID)

 GROUP BY company
 ORDER BY company;
 --summed open orders

 SELECT company, (sum (unit_price * total_qty_ord)) as total_open_orders

 FROM 
   (SELECT arc.company, od.unit_price, od.total_qty_ord, arc.credit_limit   
    FROM iqms.arcusto arc, iqms.orders o, iqms.ord_detail od
    WHERE od.orders_id = o.ID
       AND o.arcusto_id = arc.ID
       AND (od.cumm_shipped < od.total_qty_ord  OR od.cumm_shipped IS NULL))

 GROUP BY company
 ORDER BY company;
这些数字在手动检查时同样正确

我现在需要的是将这两个计算相加,并检查该数字是否大于该客户的“信用额度”字段。我已经为此编写了代码,但是数字超出了它们应该做的事情。下面给出了代码

 --summmed open invoices + open orders

  SELECT company, credit_limit, 
        round(sum(i_up * invoice_qty)) AS total_invoices, 
        round(sum (o_up * total_qty_ord)) AS total_orders,
        round(sum(i_up * invoice_qty) + sum (o_up * total_qty_ord)) as overall_total

 FROM 
   (SELECT arc.company, arc.credit_limit, ard.unit_price as i_up, ard.invoice_qty, od.unit_price as o_up, od.total_qty_ord 
    FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc, iqms.orders o, iqms.ord_detail od
    WHERE 
       ard.arprepost_id = arp.ID      
       AND arp.arcusto_id = arc.ID
       AND od.orders_id = o.ID
       AND o.arcusto_id = arc.ID
       AND (od.cumm_shipped < od.total_qty_ord  OR od.cumm_shipped IS NULL)
   )

 GROUP BY company, credit_limit
 HAVING ((sum(i_up * invoice_qty)) + (sum (o_up * total_qty_ord)) > credit_limit)
 ORDER BY company;

我不确定我错在哪里。也许这是一个简单的解决方案,也许我的逻辑有问题。非常感谢您的任何见解。非常感谢您的持续支持。

在这种情况下,由于两个查询是不同的,我建议您使用:


我的第一个观察结果是,您应该使用隐式连接,因为隐式连接已经过时20年了

您不能像以前那样组合两个查询,因为它们来自不同的表,使用隐式联接进行组合会导致交叉联接。我认为最简单的方法是对每个查询使用2个左连接:

SELECT  arc.Company,
        arc.Credit_Lime,
        COALESCE(Open_Orders, 0) AS Open_Orders,
        COALESCE(Open_Invoices, 0) AS Open_Invoices
        COALESCE(Open_Orders, 0) + COALESCE(Open_Invoices, 0) AS Total_Amount
FROM    iqms.arcusto arc
        LEFT JOIN
        (   SELECT  o.arcusto_ID
                    SUM(od.unit_price * od.total_qty_ord) AS Open_Orders
            FROM    iqms.orders o
                    INNER JOIN iqms.ord_detail od
                        ON od.orders_ID = o.ID
            WHERE   od.Cumm_Shipped < od.Total_qty_Ord
            OR      od.Cumm_Shipped IS NULL
            GROUP BY o.arcusto_ID
        ) openOrders
            ON openOrders.Arcusto_ID = arc.ID
        LEFT JOIN
        (   SELECT  arp.arcusto_id,
                    SUM(Unit_Price * Invoice_Quantity) AS Open_Invoices
            FROM    iqms.arprepost_detail ard
                    INNER JOIN iqms.arprepost arp
                        ON  ard.arprepost_id = arp.ID
            GROUP BY arp.arcusto_id
        ) OpenInvoices
            ON OpenInvoices.Arcusto_ID = arc.ID
WHERE   COALESCE(Open_Orders, 0) + COALESCE(Open_Invoices, 0) > arc.Credit_Limit;
您还可以按如下方式合并这两个查询:

SELECT  arc.Company,
        arc.Credit_Lime,
        SUM(COALESCE(Open_Orders, 0)) AS Open_Orders
FROM    iqms.arcusto arc
        LEFT JOIN
        (   SELECT  o.arcusto_ID
                    SUM(od.unit_price * od.total_qty_ord) AS Open_Orders
            FROM    iqms.orders o
                    INNER JOIN iqms.ord_detail od
                        ON od.orders_ID = o.ID
            WHERE   od.Cumm_Shipped < od.Total_qty_Ord
            OR      od.Cumm_Shipped IS NULL
            GROUP BY o.arcusto_ID
            UNION ALL
            SELECT  arp.arcusto_id,
                    SUM(Unit_Price * Invoice_Quantity) AS Open_Invoices
            FROM    iqms.arprepost_detail ard
                    INNER JOIN iqms.arprepost arp
                        ON  ard.arprepost_id = arp.ID
            GROUP BY arp.arcusto_id
        ) OpenInvoices
            ON OpenInvoices.Arcusto_ID = arc.ID
GROUP BY arc.Company, arc.Credit_Limit
HAVING  SUM(Open_Orders) > arc.Credit_Limit;

但我认为第一种方法在确定未结订单或未结发票是否超出信用限额方面提供了更大的灵活性

非常感谢,伙计,你让我免于崩溃!非常非常感谢谢谢