彙總數據基本上我需要一羣獨特的產品數據爲一行時:SQL建議 - 爲特定的情況下
- 供應商SKU匹配
- 價格的產品相媲美(或)1產品線等於'0.00'
下面是一個示例數據集集以及我正在嘗試完成的工作查詢。我不完全清楚這是執行此查詢的最佳方式。
DECLARE @Test TABLE
(
SupplierSKU VARCHAR(25),
Description VARCHAR(50),
Quantity VARCHAR(25),
Price VARCHAR(25)
)
INSERT INTO @Test
SELECT '123', 'APPLES', '15', '0.00'
INSERT INTO @Test
SELECT '124', 'ORANGES', '10', '15.34'
INSERT INTO @Test
SELECT '123', 'APPLES', '5', '27.40'
INSERT INTO @Test
SELECT '125', 'PLUMS', '67', '34.86'
INSERT INTO @Test
SELECT '124', 'ORANGES', '10', '15.78'
INSERT INTO @Test
SELECT '125', 'PLUMS', '3', '34.86'
SELECT SupplierSKU, Description, SUM(Quantity) AS [Quantity], MAX(Price) AS [Price]
FROM
(
SELECT SupplierSKU, Description, SUM(CAST(Quantity AS INT)) AS [Quantity], (SELECT MAX(CAST(Price AS MONEY)) AS [Price] FROM @Test ti WHERE ti.SupplierSKU = t.SupplierSKU AND ti.Price = t.price AND ti.Price <> '0.00') AS [Price]
FROM @Test t
GROUP BY SupplierSKU, Description, Price
) pdata
GROUP BY pdata.SupplierSKU, pdata.Description
期望的結果:
SupplierSKU Description Quantity Price
123 APPLES 20 27.40
124 ORANGES 10 15.34
124 ORANGES 10 15.78
125 PLUMS 70 34.86
假設'APPLES'還有另一行價格爲'10.00'。你想要'27 .40'還是'10.00'包含15'0.00'蘋果?你會如何決定?我認爲您當前的查詢甚至不會按SKU/Price對這種情況進行正確分組。 –
謝天謝地,系統不會產生這樣的數據。新的'APPLES'行將有不同的SupplierSKU。 – Jesse
橘子應該返回10/15.78?或20/15.78?你提到的價格應該匹配(或0) –