我在加入表格时遇到一个常见问题,每个条件我得到两个结果,我需要消除一个。
例如,我现在正试图找出我拥有的产品代码的库存以及是否有任何订单以及到期时间。我只想查看每个产品代码一次,并且在我刚刚展示时它工作正常订购的数量,但现在我想要我必须做左连接的截止日期。现在我得到每个产品代码的两个结果。我得到一个显示它有什么库存,另一个显示有什么东西在订购以及何时到期。我只想查看库存,如果没有订单,如果有订单,库存和订单作为一条线,而不是两者分开。
DECLARE @cat VARCHAR(150)
SET @cat = 'crumb' --Put your search string in the single speech marks
SELECT category_treeview_node.ctn_description AS
[Product Category],
variant_detail.vad_variant_code AS [Variant Code],
variant_detail.vad_description AS
[Variant Description],
Sum(variant_transaction_header.vth_current_quantity) AS [Physical Stock],
variant_stock_quantity.vasq_free_stock_quantity AS [Free Stock],
variant_stock_location.vsl_stock_on_purchase_order AS [Stock on order],
purchase_order_header.poh_required_date AS [Due Date]
FROM variant_stock_quantity
INNER JOIN (variant_detail
INNER JOIN variant_category
ON variant_detail.vad_id =
variant_category.vac_vad_id
INNER JOIN category_treeview_node
ON category_treeview_node.ctn_id =
variant_category.vac_ctn_id)
ON variant_stock_quantity.vasq_vad_id = variant_detail.vad_id
INNER JOIN variant_stock_location
ON variant_detail.vad_id = variant_stock_location.vsl_vad_id
LEFT JOIN (variant_transaction_header_total
INNER JOIN variant_transaction_header
ON variant_transaction_header_total.vtht_vth_id =
variant_transaction_header.vth_id)
ON variant_detail.vad_id = variant_transaction_header.vth_vad_id
LEFT JOIN purchase_order_line
ON purchase_order_line.pol_id =
variant_transaction_header.vth_pol_id
AND variant_detail.vad_id = purchase_order_line.pol_vad_id
LEFT JOIN purchase_order_header
ON purchase_order_header.poh_id = purchase_order_line.pol_poh_id
WHERE category_treeview_node.ctn_description = @cat
GROUP BY category_treeview_node.ctn_description,
variant_detail.vad_variant_code,
variant_detail.vad_description,
variant_stock_quantity.vasq_free_stock_quantity,
variant_stock_location.vsl_stock_on_purchase_order,
purchase_order_header.poh_required_date
HAVING variant_stock_quantity.vasq_free_stock_quantity >= 0
我得到一个结果,它显示了实物库存、免费库存、订单中的内容和到期日期为空,然后一个结果显示没有实物库存、免费库存、订单中的内容和到期日。
任何帮助表示赞赏。