0

我在下面有这个查询,我在超市数据库上使用它来查看使用 SQL Server 2008 可在线销售 SAP DB 的商品。

SELECT T0.[CodeBars], T0.[ItemName], T3.[Price], T1.[Price], T2.[ItmsGrpNam]
FROM OITM T0 INNER JOIN ITM1 T1 ON T0.ItemCode = T1.ItemCode 
INNER JOIN OITB T2 ON T0.ItmsGrpCod = T2.ItmsGrpCod 
INNER JOIN ITM1 T3 ON T0.ItemCode = T3.ItemCode 
WHERE T0.[U_Online] = 'Y' AND T1.[PriceList] = '3' AND T3.[Pricelist]='1'

查询工作正常,但我想为 OnHand 项目添加另一列。即特定仓库“6”的库存物品,所有在线销售的物品都来自该仓库。超市有8个仓库。显示在线销售的T0.[U_Online] = 'Y'商品,无论仓库如何。

如何修改下面的查询,以便对于每个T0.[CodeBars]退货,我都会为仓库 6 的 Onhand items 获得一个额外的列?我对表 OITW 有一个额外的连接。

SELECT T0.[CodeBars], T0.[ItemName], T3.[Price], T1.[Price], T2.[ItmsGrpNam],
T4.OnHand
FROM OITM T0 INNER JOIN ITM1 T1 ON T0.ItemCode = T1.ItemCode 
INNER JOIN OITB T2 ON T0.ItmsGrpCod = T2.ItmsGrpCod 
INNER JOIN ITM1 T3 ON T0.ItemCode = T3.ItemCode 
INNER JOIN OITW T4 ON T0.ItemCode = T4.ItemCode
WHERE T0.[U_Online] = 'Y' AND T1.[PriceList] = '3' AND T3.[Pricelist]='1'
--AND T4.WhsCode = '6'

第一次查询的示例结果

4

1 回答 1

1

我认为您可能需要LEFT JOIN这样:

SELECT 
  T0.[CodeBars], 
  T0.[ItemName], 
  COALESCE(T3.[Price], T1.[Price]) AS Price, 
  T2.[ItmsGrpNam],
  T4.OnHand
FROM       OITM T0 
LEFT JOIN ITM1 T1 ON T0.ItemCode   = T1.ItemCode   AND T1.[PriceList] = '3'
LEFT JOIN OITB T2 ON T0.ItmsGrpCod = T2.ItmsGrpCod 
LEFT JOIN ITM1 T3 ON T0.ItemCode   = T3.ItemCode   AND T3.[Pricelist] = '1'
LEFT JOIN OITW T4 ON T0.ItemCode   = T4.ItemCode   AND T4.WhsCode = '6'
WHERE T0.[U_Online] = 'Y' 
于 2013-02-05T08:11:32.867 回答