我有一个场景,我们有两个系统都包含相关的PO - 一个是全局系统,一个是本地系统。我需要编写2个显示1的查询:在全局系统中取消但在本地系统中没有取消2.在本地系统中取消但在全局系统中取消。棘手的部分是全局系统将始终只有1个行项目,其状态为90表示取消或10表示活动。本地端的相关采购订单可以有多个行项目,如果其中任何一个处于活动状态,则采购订单处于活动状态。我为第一个场景编写了下面的SQL,它运行得很好,因为如果本地端的任何单个PO行项目处于活动状态,则PO处于活动状态。现在我需要做相反的事情而且我遇到了麻烦,因为我不知道如何说“只有当所有线路都关闭时,PO才会关闭” - 任何想法。欣赏帮助,仍然学习如何使用标签。本地“L”是行的代码被删除 - 空白将被激活。
第一个有效的场景的查询是:
SELECT G.order_no AS 'GPS_ORDER_#',
G.order_status AS 'GPS_ORDER_STATUS',
G.cst_order_no AS 'GPS_CUSTOMER_PO_#',
H.PO_NUMBER AS 'SAP_PO_#',
P.PO_ITEM_NUMBER,
P.DEL_INDICATOR
FROM (SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.SimoxOrder1
UNION ALL
SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.SimoxOrder2
UNION ALL
SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.SimoxOrder3) G
JOIN PDX_SAP_USER.dbo.VW_PO_HEADER H
ON G.order_no = H.AHAG_NUMBER
JOIN PDX_SAP_USER.dbo.VW_PO_ITEM P
ON H.PO_NUMBER = P.PO_NUMBER
WHERE G.order_status = '90'
AND P.DEL_INDICATOR <> 'L';
答案 0 :(得分:0)
第1步:
SELECT ... CASE WHEN DEL_INDICATOR = 'L' THEN TRUE ELSE FALSE END AS DEL_INDICATOR_BOOL
第2步:
... GROUP BY P.PO_NUMBER HAVING BOOL_AND(DEL_INDICATOR_BOOL) == FALSE
用文字说明:
可以使用group by + having + aggregate function
答案 1 :(得分:0)
如果全局采购订单始终至少有1个匹配的本地采购订单,那么您可以在状态为活动状态的本地采购订单上执行子查询并加入,然后查找本地采购订单的空值。如果没有评论,我将不会尝试更改您的查询,但它看起来像是:
SELECT
global_table.po
FROM global_table
LEFT JOIN (
SELECT
PO
FROM local_table
WHERE local_table.po_status = 90 --Looking for any active POs
) AS local_table ON
global_table.po = local_table.po --Join those active POs
WHERE local_table.po IS NULL --Exclude any POs that had an active record in the local table
答案 2 :(得分:0)
如果使用以下查询中的CASE语句全部为“L”,则检查del_indicator的值
SELECT g.order_no AS 'gps_order_#',
g.order_status AS 'gps_order_status',
g.cst_order_no AS 'gps_customer_po_#',
l.po_number AS 'sap_po_#',
l.po_item_number,
l.del_indicator
FROM (SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.simoxorder1
UNION ALL
SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.simoxorder2
UNION ALL
SELECT order_no,
order_status,
cst_order_no
FROM asagdwpdx_prod.dbo.simoxorder3) g
JOIN (SELECT h.ahag_number,
h.po_number,
i.po_item_number,
i.del_indicator,
CASE WHEN MIN(i.del_indicator) = MAX(i.del_indicator)
AND MIN(i.del_indicator) = 'L'
THEN 'closed'
ELSE 'active'
END local_o_status
FROM pdx_sap_user.dbo.vw_po_header h
JOIN pdx_sap_user.dbo.vw_po_item i
ON h.po_number = i.po_number
GROUP BY h.ahag_number,
h.po_number,
i.po_item_number,
i.del_indicator) l
ON g.order_no = l.ahag_number
WHERE g.order_status = '10'
AND l.local_o_status = 'closed'