5

刚开始实习,并负责一些 SQL。发现它有些棘手。感谢任何帮助。谢谢!

任务:

“是否可以为以下内容编写警报。显示所有客户,当下个月到期的未结销售订单与未结销售发票合并(添加)时,他们超出了他们的信用额度。”

我提出的逻辑:

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

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

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

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

如果 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 进行比较?

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

4

1 回答 1

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,因此上述查询“重复” 和 中的SELECT总和HAVING。这可以通过嵌套来消除,但请注意HAVING是用 . 切换的WHERE。所以查询的 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;
于 2012-11-23T11:23:47.100 回答