SQL;求和2个字段,然后将它们一起添加

时间:2012-11-23 11:15:06

标签: sql database oracle sum

刚刚开始实习并且已经完成了一些SQL任务。发现它有点棘手。感谢任何帮助。谢谢!

任务:

“是否可以为以下内容撰写提醒。显示所有客户,当下个月到期的未结销售订单与开放式销售发票合并(添加)时,超出其信用额度。”

我建议的逻辑:

  1. 首先,我要做的是在下个月内为每个客户(在SALES表中)汇总所有销售订单。

  2. 接下来,为每个客户汇总所有未结发票,即计算每个客户欠款的总金额(在INVOICES表中)。

  3. 然后我想通过相应的customer_id将1和2的结果加在一起

  4. 接下来比较上面3中的计算,看看它是否大于每个客户的信用额度。

  5. 如果3中的总和超过信用额度,则只应在生成的表格中显示这些公司。结果表的理想格式是

    Cust_ID|Name|Sum_sales_orders (1.above)|Sum_open_invoices (2.above)| Total_orders&invoices
    ----------------------------------------------------------------------------
       1   |  A |  25000                   |  333                      | 25333 
           |    |                          |                           |
    

    到目前为止我的代码是

     SELECT arc.company, arc.credit_limit, 
         sum (ard.unit_price * ard.invoice_qty) as open_invoice_total, 
         sum (od.total_qty_ord * od.unit_price) as open_orders_total
     FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc, iqms.ord_detail od, iqms.orders o
     WHERE ard.arprepost_id = arp.id 
         and arc.id = o.arcusto_id and o.id = od.orders_id
         and arp.arcusto_id = arc.id
      GROUP BY arc.company, arc.credit_limit
    

    我认为这是在sum()函数中计算正确的总数?或者我错了?如何添加open_invoice_total和open_orders_total?然后将它们与credit_limit进行比较?

    我希望你们明白我要做的事情。我在这里先向您的帮助表示感谢! :)

1 个答案:

答案 0 :(得分:3)

您可以使用HAVING子句检查总计的总和是否超过信用额度,如此。为了便于阅读,我还用INNER JOINS替换了WHERE子句中的连接。

 SELECT arc.company, arc.credit_limit, 
       sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
       sum(od.total_qty_ord * od.unit_price) as open_orders_total
 FROM iqms.arprepost_detail ard 
        INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
        INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
        INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
        INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
  GROUP BY arc.company, arc.credit_limit
  HAVING sum(ard.unit_price * ard.invoice_qty) + sum(od.total_qty_ord * od.unit_price) > arc.credit_limit;

修改

为了澄清Dems评论,GROUP BY不允许aliases的规范,因此上述查询'重复'SELECTHAVING中的总和}。这可以通过嵌套来消除,但请注意HAVINGWHERE切换。所以查询的DRYer版本是:

SELECT company, credit_limit, open_invoice_total, open_orders_total
FROM
(
     SELECT arc.company, arc.credit_limit, 
           sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
           sum(od.total_qty_ord * od.unit_price) as open_orders_total
     FROM iqms.arprepost_detail ard 
            INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
            INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
            INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
            INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
      GROUP BY arc.company, arc.credit_limit
) AS nested
WHERE (open_invoice_total + open_orders_total) > arc.credit_limit;

而且,如果您愿意,CTE可能会使其更具可读性:

;WITH nested AS
(
     SELECT arc.company, arc.credit_limit, 
           sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
           sum(od.total_qty_ord * od.unit_price) as open_orders_total
     FROM iqms.arprepost_detail ard 
            INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
            INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
            INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
            INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
      GROUP BY arc.company, arc.credit_limit
)
SELECT company, credit_limit, open_invoice_total, open_orders_total
  FROM nested
  WHERE (open_invoice_total + open_orders_total) > arc.credit_limit;