SELECT FBA IF QTY> 0 ELSE使用FBM

时间:2014-10-25 13:39:38

标签: sql-server tsql

我想输出库存以加载到亚马逊库存装载程序。如果我有FBA股票,那么该行应该是FBA,但如果没有FBA股票,那么该行应该是FBM。

FBA和FBM是StockLocation.Location表中的两个股票仓位值。

[sku], [product-id], [price], [fulfillment-center-id], [quantity]
[AAA], [0001], [0.10], [AMAZON_EU], [10]
[AAA], [0001], [0.10], [DEFAULT], [1]
[BBB], [000], [0.10], [AMAZON_EU], [0]
[BBB], [000], [0.10], [DEFAULT], [1]

所以基于上面的结果,结果将如下所示;

[sku], [product-id], [price], [fulfillment-center-id], [quantity]
[AAA], [0001], [0.10], [AMAZON_EU], [10]
[BBB], [000], [0.10], [DEFAULT], [1]

这是我到目前为止所拥有的;

DECLARE @source nvarchar(255) = 'Amazon'
DECLARE @subSource nvarchar(255) = 'Store1'
DECLARE @FBM nvarchar(255) = 'DEFAULT'
DECLARE @FBA nvarchar(255) = 'AMAZON_EU'
SELECT    T1.ItemNumber AS [sku],
  T1.BarcodeNumber AS [product-id],
  T3.SalePrice AS [price],
  T5.Location AS [fulfillment-center-id],
  T4.Quantity AS [quantity]
FROM [stockitem] T1
INNER JOIN StockItem_Titles T2 on T1.pkStockItemID = T2.fkStockItemId
INNER JOIN StockItem_Pricing T3 on T1.pkStockItemID = T3.fkStockItemId AND T2.Source =     T3.Source AND T2.SubSource = T2.SubSource
INNER JOIN StockLevel T4 on T1.pkStockItemID = T4.fkStockItemId
LEFT OUTER JOIN StockLocation T5 on T4.fkStockLocationId = T5.pkStockLocationId
WHERE T3.Source = @source AND T3.SubSource = @subSource

因为它会为每个sku输出两行,所以我需要将其减少为FBA OR FBM

如果它有助于数据库是LINNWORKS。

提前感谢您的帮助和指导

斯图

LinnWorks Database Structure

2 个答案:

答案 0 :(得分:0)

这是我提出的解决方案,如果你有另一个或者确实更好或更正确的方式,那么请发布它。我使用MIN来按字母顺序将FBA超过FBM,不确定它是最好的方法,但它看起来有效。

DECLARE @source nvarchar(255) = 'Amazon'
DECLARE @subSource nvarchar(255) = 'Store1'
DECLARE @FBA nvarchar(255) = 'AMAZON_EU'
DECLARE @FBM nvarchar(255) = 'Default'

SELECT  T1.ItemNumber AS [sku],
    T1.BarcodeNumber AS [product-id],
    'EAN' AS [product-id-type],
    T3.SalePrice AS [price],
    '' AS [minimum-seller-allowed-price],
    '' AS [maximum-seller-allowed-price],
    'New' AS [item-condition],
    CASE 
        WHEN T5.Location = @FBA THEN NULL
        ELSE T4.Quantity
    END AS [quantity],
    CASE
        WHEN Quantity <= 0 THEN 'x'
        ELSE 'a'
    END AS [add-delete],
    6 AS [will-ship-internationally],
    3 AS [expedited-shipping],
    'Message' AS [item-note],
    MIN(T5.Location) AS [fulfillment-center-id],
    CASE 
        WHEN T5.Location = @FBA THEN 'AMAZON_EU'
        ELSE 'Default'
    END AS [fulfillment-center-id]

FROM [stockitem] T1
INNER JOIN StockItem_Titles T2 on T1.pkStockItemID = T2.fkStockItemId
INNER JOIN StockItem_Pricing T3 on T1.pkStockItemID = T3.fkStockItemId AND T2.Source =     T3.Source AND T2.SubSource = T2.SubSource
INNER JOIN StockLevel T4 on T1.pkStockItemID = T4.fkStockItemId AND T4.Quantity <> 0
LEFT JOIN StockLocation T5 on T4.fkStockLocationId = T5.pkStockLocationId
WHERE   T3.Source = @source AND T3.SubSource = @subSource AND LEN(T1.BarcodeNumber) > 5
GROUP BY T1.ItemNumber, T1.BarcodeNumber, T3.SalePrice, T4.Quantity, T5.Location

答案 1 :(得分:0)

我怀疑和T2.SubSource = T2.SubSource错误或不需要
将条件拉入连接有时可以帮助查询优化器

DECLARE @source nvarchar(255) = 'Amazon'
DECLARE @subSource nvarchar(255) = 'Store1'
DECLARE @FBA nvarchar(255) = 'AMAZON_EU'
DECLARE @FBM nvarchar(255) = 'Default'

SELECT T1.ItemNumber AS [sku],
       T1.BarcodeNumber AS [product-id],
       'EAN' AS [product-id-type],
       T3.SalePrice AS [price],
       '' AS [minimum-seller-allowed-price],
       '' AS [maximum-seller-allowed-price],
       'New' AS [item-condition],
       CASE 
          WHEN T5.Location = @FBA THEN NULL
          ELSE T4.Quantity
       END AS [quantity],
       CASE
          WHEN Quantity <= 0 THEN 'x'
          ELSE 'a'
       END AS [add-delete],
       6 AS [will-ship-internationally],
       3 AS [expedited-shipping],
       'Message' AS [item-note],
       MIN(T5.Location) AS [fulfillment-center-id],
       CASE 
          WHEN T5.Location = @FBA THEN 'AMAZON_EU'
          ELSE 'Default'
       END AS [fulfillment-center-id]

 FROM [stockitem] T1
 JOIN StockItem_Titles T2 
   on T1.pkStockItemID = T2.fkStockItemId
  AND LEN(T1.BarcodeNumber) > 5
 JOIN StockItem_Pricing T3 
   on T1.pkStockItemID = T3.fkStockItemId 
  AND T2.Source = T3.Source 
  AND T2.SubSource = T2.SubSource
  and T3.Source = @source 
  AND T3.SubSource = @subSource
 JOIN StockLevel T4 
   on T1.pkStockItemID = T4.fkStockItemId 
  AND T4.Quantity <> 0
 left JOIN StockLocation T5 
   on T4.fkStockLocationId = T5.pkStockLocationId    
GROUP BY T1.ItemNumber, T1.BarcodeNumber, T3.SalePrice, T4.Quantity, T5.Location