我有一份供应商的零件清单,我希望看到自1月1日以来我们买卖的东西。我为销售和采购信息创建了子查询。
结果有四种可能性:
如果销售和采购数量均为NULL,如何保留那些可能具有NULL销售数量或采购数量的行,我如何排除行?附上图像以显示这些场景的示例。
我审核了this帖子,但它似乎不适用,因为我的列不必相等。
SELECT DISTINCT
p21_view_supplier.supplier_id AS [Supp ID],
p21_view_supplier.supplier_name AS Supplier,
p21_view_inv_mast.item_id AS [Item ID],
p21_view_inv_mast.item_desc AS [Item Desc],
p21_view_inv_loc.location_id AS [Location ID],
invoice_data.[Total Shipped by Sales Loc],
purchase_data.[PO Qty Ordered]
FROM
p21_view_supplier
INNER JOIN p21_view_inventory_supplier ON p21_view_supplier.supplier_id = p21_view_inventory_supplier.supplier_id
INNER JOIN p21_view_inv_mast ON p21_view_inventory_supplier.inv_mast_uid = p21_view_inv_mast.inv_mast_uid
INNER JOIN p21_view_inv_loc ON p21_view_inv_mast.inv_mast_uid = p21_view_inv_loc.inv_mast_uid
--Invoices
LEFT JOIN (
SELECT
p21_view_invoice_hdr.sales_location_id,
p21_view_invoice_line.item_id,
p21_view_invoice_line.supplier_id,
p21_view_invoice_line.inv_mast_uid,
SUM(p21_view_invoice_line.qty_shipped) AS [Total Shipped by Sales Loc]
FROM
p21_view_invoice_hdr
INNER JOIN p21_view_invoice_line ON p21_view_invoice_hdr.invoice_no = p21_view_invoice_line.invoice_no
WHERE
(p21_view_invoice_hdr.invoice_date >= CONVERT(DATETIME, '2016-01-01 00:00:00', 102))
GROUP BY
p21_view_invoice_hdr.sales_location_id,
p21_view_invoice_line.item_id,
p21_view_invoice_line.supplier_id,
p21_view_invoice_line.inv_mast_uid
) invoice_data ON p21_view_supplier.supplier_id = invoice_data.supplier_id
AND p21_view_inv_mast.inv_mast_uid = invoice_data.inv_mast_uid
AND p21_view_inv_loc.location_id = invoice_data.sales_location_id
--Purchasing
LEFT JOIN (
SELECT
p21_view_po_hdr.supplier_id,
p21_view_po_hdr.location_id AS [PO Loc ID],
SUM(p21_view_po_line.qty_ordered) AS [PO Qty Ordered],
p21_view_po_line.item_id AS [Item ID],
p21_view_po_line.inv_mast_uid
FROM
p21_view_po_hdr
INNER JOIN p21_view_po_line ON p21_view_po_hdr.po_no = p21_view_po_line.po_no
WHERE
(p21_view_po_hdr.date_created >= CONVERT(DATETIME, '2016-01-01 00:00:00', 102))
AND (p21_view_po_hdr.delete_flag IS NULL OR
p21_view_po_hdr.delete_flag = 'N')
AND (p21_view_po_line.delete_flag IS NULL OR
p21_view_po_line.delete_flag = 'N')
AND (p21_view_po_line.cancel_flag IS NULL OR
p21_view_po_line.cancel_flag = 'N')
AND (p21_view_po_hdr.cancel_flag IS NULL OR
p21_view_po_hdr.cancel_flag = 'N')
GROUP BY
p21_view_po_hdr.supplier_id,
p21_view_po_hdr.location_id,
p21_view_po_line.item_id,
p21_view_po_line.inv_mast_uid
) purchase_data ON p21_view_supplier.supplier_id = purchase_data.supplier_id
AND p21_view_inv_mast.inv_mast_uid = purchase_data.inv_mast_uid
AND p21_view_inv_loc.location_id = purchase_data.[PO Loc ID]
WHERE
(p21_view_supplier.supplier_id = 8761)
AND (p21_view_inv_mast.delete_flag = 'N')
ORDER BY
p21_view_inv_mast.item_id,
p21_view_inv_loc.location_id
答案 0 :(得分:0)
假设两个派生表中至少有一个不可为空的列,您可以简单地向外部查询where子句添加一个条件,只有在派生的至少一个非可空列中才会求值为true表格不为空。
在这种情况下,我假设两个派生表的非可空列都相同,并且它是supplier_id
,所以我在where子句中添加了以下条件:
AND NOT (purchase_data.supplier_id is null and invoice_data.supplier_id is null)
完整查询:
SELECT DISTINCT
p21_view_supplier.supplier_id AS [Supp ID],
p21_view_supplier.supplier_name AS Supplier,
p21_view_inv_mast.item_id AS [Item ID],
p21_view_inv_mast.item_desc AS [Item Desc],
p21_view_inv_loc.location_id AS [Location ID],
invoice_data.[Total Shipped by Sales Loc],
purchase_data.[PO Qty Ordered]
FROM
p21_view_supplier
INNER JOIN p21_view_inventory_supplier ON p21_view_supplier.supplier_id = p21_view_inventory_supplier.supplier_id
INNER JOIN p21_view_inv_mast ON p21_view_inventory_supplier.inv_mast_uid = p21_view_inv_mast.inv_mast_uid
INNER JOIN p21_view_inv_loc ON p21_view_inv_mast.inv_mast_uid = p21_view_inv_loc.inv_mast_uid
--Invoices
LEFT JOIN (
SELECT
p21_view_invoice_hdr.sales_location_id,
p21_view_invoice_line.item_id,
p21_view_invoice_line.supplier_id,
p21_view_invoice_line.inv_mast_uid,
SUM(p21_view_invoice_line.qty_shipped) AS [Total Shipped by Sales Loc]
FROM
p21_view_invoice_hdr
INNER JOIN p21_view_invoice_line ON p21_view_invoice_hdr.invoice_no = p21_view_invoice_line.invoice_no
WHERE
(p21_view_invoice_hdr.invoice_date >= CONVERT(DATETIME, '2016-01-01 00:00:00', 102))
GROUP BY
p21_view_invoice_hdr.sales_location_id,
p21_view_invoice_line.item_id,
p21_view_invoice_line.supplier_id,
p21_view_invoice_line.inv_mast_uid
) invoice_data ON p21_view_supplier.supplier_id = invoice_data.supplier_id
AND p21_view_inv_mast.inv_mast_uid = invoice_data.inv_mast_uid
AND p21_view_inv_loc.location_id = invoice_data.sales_location_id
--Purchasing
LEFT JOIN (
SELECT
p21_view_po_hdr.supplier_id,
p21_view_po_hdr.location_id AS [PO Loc ID],
SUM(p21_view_po_line.qty_ordered) AS [PO Qty Ordered],
p21_view_po_line.item_id AS [Item ID],
p21_view_po_line.inv_mast_uid
FROM
p21_view_po_hdr
INNER JOIN p21_view_po_line ON p21_view_po_hdr.po_no = p21_view_po_line.po_no
WHERE
(p21_view_po_hdr.date_created >= CONVERT(DATETIME, '2016-01-01 00:00:00', 102))
AND (p21_view_po_hdr.delete_flag IS NULL OR
p21_view_po_hdr.delete_flag = 'N')
AND (p21_view_po_line.delete_flag IS NULL OR
p21_view_po_line.delete_flag = 'N')
AND (p21_view_po_line.cancel_flag IS NULL OR
p21_view_po_line.cancel_flag = 'N')
AND (p21_view_po_hdr.cancel_flag IS NULL OR
p21_view_po_hdr.cancel_flag = 'N')
GROUP BY
p21_view_po_hdr.supplier_id,
p21_view_po_hdr.location_id,
p21_view_po_line.item_id,
p21_view_po_line.inv_mast_uid
) purchase_data ON p21_view_supplier.supplier_id = purchase_data.supplier_id
AND p21_view_inv_mast.inv_mast_uid = purchase_data.inv_mast_uid
AND p21_view_inv_loc.location_id = purchase_data.[PO Loc ID]
WHERE
(p21_view_supplier.supplier_id = 8761)
AND (p21_view_inv_mast.delete_flag = 'N')
AND NOT (purchase_data.supplier_id is null and invoice_data.supplier_id is null)
ORDER BY
p21_view_inv_mast.item_id,
p21_view_inv_loc.location_id