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

时间:2012-11-28 15:23:49

标签: sql database oracle sum

好的家伙关于这个话题的最后一个问题(I HOPE)。

我需要做的是汇总来自两个不同表格的字段,将它们加在一起,并检查此总额是否大于客户信用额度。

我已将问题分解为较小的任务,以便我可以尝试提出整体解决方案。 (这些较小的代码对我来说正常工作,但是当在第三段代码中合并时,它们总和不正确)。首先,我计算了第一个求和字段,如下所示:

 --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;

这些数字再次在人工检查中正确显示。

我现在需要的是将这两个计算加在一起并检查该数字是否大于该客户的'credit_limit'字段。我已经为此编写了代码,但是这些数字已经超出了它们的应用范围(下面给出了这段代码)。

 --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;

我不确定我哪里出错了。也许它是一个简单的修复或者我的逻辑可能有问题。非常感谢任何见解。非常感谢您的继续支持。

2 个答案:

答案 0 :(得分:1)

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

SELECT company, SUM(open_invoices) total /*should rename the column in query 1*/ 
  FROM ([QUERY1]
        UNION ALL
        [QUERY2])
 GROUP by company

答案 1 :(得分:1)

我的第一个观察是你应该使用explicit joins instead of implicit joins,暗示连接已经过时了20年

您不能像以前那样组合2个查询,因为它们来自不同的表,使用隐式连接这样做会导致交叉连接。我认为最简单的方法是为每个查询使用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;

但我认为第一种方法可以更灵活地确定Open_Orders或Open_Invoices是否超出信用额度