我有一个存储过程
SELECT [ShipWorks].[dbo].[OrderItem].[Code] AS Code,
CASE WHEN (LEN(ISNULL(MAX([ShipWorks].[dbo].[OrderItem].[Location]),'')) = 1)
THEN MAX([ShipWorks].[dbo].[OrderItem].[Location])
ELSE MAX([Picklist].[dbo].[ItemData].[InventoryLocation])
END AS Location,
SUM([ShipWorks].[dbo].[OrderItem].[Quantity]) AS Quantity,
MAX(LEFT([ShipWorks].[dbo].[Store].[StoreName], 1)) AS Store
FROM [ShipWorks].[dbo].[OrderItem]
INNER JOIN [ShipWorks].[dbo].[Order] ON [ShipWorks].[dbo].[OrderItem].[OrderID] = [ShipWorks].[dbo].[Order].[OrderID]
INNER JOIN [ShipWorks].[dbo].[Store] ON [ShipWorks].[dbo].[Order].[StoreID] = [ShipWorks].[dbo].[Store].[StoreID]
LEFT JOIN [ShipWorks].[dbo].[AmazonOrder] ON [ShipWorks].[dbo].[AmazonOrder].[OrderID] = [ShipWorks].[dbo].[Order].[OrderID]
LEFT JOIN [Picklist].[dbo].[ItemData] ON [Picklist].[dbo].[ItemData].[InventoryNumber] = [ShipWorks].[dbo].[OrderItem].[Code] OR
[Picklist].[dbo].[ItemData].[MediaCreator] = [OrderItem].[Code]
WHERE [ShipWorks].[dbo] [Order].[LocalStatus] = 'Recently Downloaded' AND ([ShipWorks].[dbo].[AmazonOrder].[FulfillmentChannel] = 2 OR [ShipWorks].[dbo].[Store].[StoreName] <> 'Amazon')
GROUP BY [ShipWorks].[dbo].[OrderItem].[Code]
ORDER BY Location
返回这个:
Code Location Quantity Store
L698-W-EA NULL 2 A
L82009-EA A1K2, A1N2, C4Y3, CBP2 2 A
L80401-A-EA A1S2, SHIP, R2F1, CBP5, BRP, BRP1-20 17 A
CWD2132W-BOX-25PK A-AISLE 1 M
GM22660003-EA B1K2 1 M
我想将 Location 列拆分为多个列。
Code loc1 loc2 loc3 loc4 loc5 loc6 Quantity Store
L698-W-EA NULL 2 A
L82009-EA A1K2, A1N2, C4Y3, CBP2 2 A
L80401-A-EA A1S2, SHIP, R2F1, CBP5, BRP, BRP1-20 17 A
CWD2132W-BOX-25PK A-AI 1 M
GM22660003-EA B1K2 1 M
我找到了这个答案,但那是针对postgresql的。也是这个,但它返回一个包含多行的表,您可以选择前 1 个,但我想要所有结果,我不确定如何在存储过程中使用 tvf Split(string, ',') 来获取我想要的结果集?
此选择语句导致错误: “无法对包含聚合或子查询的表达式执行聚合函数。”
SELECT [ShipWorks].[dbo].[OrderItem].[Code] AS Code,
CASE WHEN (LEN(ISNULL(MAX([ShipWorks].[dbo].[OrderItem].[Location]),'')) = 1)
THEN MAX((SELECT TOP 1 i.* FROM dbo.Split([ShipWorks].[dbo].[OrderItem].[Location], ',') as i))
ELSE MAX((SELECT TOP 1 i.* FROM dbo.Split([ShipWorks].[dbo].[OrderItem].[Location], ',') as i))
END AS Location,
SUM([ShipWorks].[dbo].[OrderItem].[Quantity]) AS Quantity,
MAX(LEFT([ShipWorks].[dbo].[Store].[StoreName], 1)) AS Store
FROM [ShipWorks].[dbo].[OrderItem]
INNER JOIN [ShipWorks].[dbo].[Order] ON [ShipWorks].[dbo].[OrderItem].[OrderID] = [ShipWorks].[dbo].[Order].[OrderID]
INNER JOIN [ShipWorks].[dbo].[Store] ON [ShipWorks].[dbo].[Order].[StoreID] = [ShipWorks].[dbo].[Store].[StoreID]
LEFT JOIN [ShipWorks].[dbo].[AmazonOrder] ON [ShipWorks].[dbo].[AmazonOrder].[OrderID] = [ShipWorks].[dbo].[Order].[OrderID]
LEFT JOIN [Picklist].[dbo].[ItemData] ON [Picklist].[dbo].[ItemData].[InventoryNumber] = [ShipWorks].[dbo].[OrderItem].[Code] OR
[Picklist].[dbo].[ItemData].[MediaCreator] = [OrderItem].[Code]
WHERE [Order].[LocalStatus] = 'Recently Downloaded' AND ([ShipWorks].[dbo].[AmazonOrder].[FulfillmentChannel] = 2 OR [ShipWorks].[dbo].[Store].[StoreName] <> 'Amazon')
GROUP BY [ShipWorks].[dbo].[OrderItem].[Code]
ORDER BY Location
利用 Jeff Moden 的 Tally-Ho!来自这里的 CSV 拆分器:
我们可以将解决方案编码为针对 Jeff 函数的应用和如下所示的枢轴:
产生这个: