gpt4 book ai didi

mysql - 带有计算字段的 SQL 连接表

转载 作者:行者123 更新时间:2023-11-29 02:43:16 25 4
gpt4 key购买 nike

我想在此查询中添加另一列以显示需要处理的延期交货。

当前该表显示所有具有未清项目的订单以及包含的每个库存引用的库存量。

我想为建议的发票金额添加一个字段。即如果订单数量为5,发票数量为2,库存数量为2则建议发货2。

如果进货数量是100则建议发货3。

我目前的尝试如下,但我不知道如何做最后一点。

我知道我需要做 (qtyOrdered-qtyinvoiced) 但真的碰壁了。

SELECT 
c.cus_name,
ol.orderLines_stockRef,
ol.orderLines_qtyOrdered,
ol.orderlines_qtyInvoiced,
pr.products_instock
FROM
cus c
JOIN
orderheader oh ON oh.orderHeader_customer = c.cus_id
JOIN
orderlines ol ON ol.orderLines_orderId = oh.orderHeader_id
JOIN
products pr ON ol.orderlines_stockref = pr.products_reference
WHERE
ol.orderLines_qtyOrdered <> ol.orderlines_qtyInvoiced

当前输出:

Output

最佳答案

试试这个:

SELECT 
c.cus_name,
ol.orderLines_stockRef,
ol.orderLines_qtyOrdered,
ol.orderlines_qtyInvoiced,
pr.products_instock,
(CASE WHEN ol.orderLines_qtyOrdered - ol.orderlines_qtyInvoiced > pr.products_instock
THEN pr.products_instock
ELSE ol.orderLines_qtyOrdered - ol.orderlines_qtyInvoiced) AS qtyToSend,
FROM
cus c
JOIN
orderheader oh ON oh.orderHeader_customer = c.cus_id
JOIN
orderlines ol ON ol.orderLines_orderId = oh.orderHeader_id
JOIN
products pr ON ol.orderlines_stockref = pr.products_reference
WHERE
ol.orderLines_qtyOrdered <> ol.orderlines_qtyInvoiced

关于mysql - 带有计算字段的 SQL 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com