SQL SUM OVER PARTITION BY 计算组的百分比
SQL SUM OVER PARTITION BY Calculate % of group
谁能告诉我为什么下面的代码无法编译?
我正在尝试计算 'M3' 标记的交易占整个批次的百分比。
我认为如果只有一批就足够容易了,但有时会有多批。
完整代码如下:
SELECT
[Batch],
[DocNum],
[Date],
[Time],
[Location],
[ItemCode],
[Brand],
[Quantity],
CASE
WHEN [Variance] IS NULL
THEN ''
ELSE [Variance]
END AS [Variance],
CASE
WHEN [Comments] IS NULL
THEN ''
ELSE [Comments]
END AS [Comments],
CASE
WHEN [Variance] = 'M3'
THEN(
(
(SELECT
ABS([Quantity])
FROM #BatchReport
WHERE [Variance] = 'M3')
/
(SELECT
SUM([Quantity])
FROM #BatchReport
WHERE [Quantity] > 0)
) * 100) OVER (PARTITION BY [Batch])
ELSE '0'
END AS [Pct Loss],
SUM([Quantity]) OVER (PARTITION BY [Batch]) AS [Difference],
((SUM([Quantity]) - MAX([Quantity])) * 100) / MAX([Quantity]) OVER (PARTITION BY [Batch]) AS [Pct Difference]
FROM #BatchReport
WHERE [DocNum] IS NOT NULL
GROUP BY [Batch], [DocNum], [Date], [Time], [Variance], [Brand], [Comments], [Quantity], [Location], [ItemCode]
ORDER BY [Batch], [Date], [Time]
错误文本:Incorrect syntax near the keyword 'OVER'.
如果我删除这部分,错误就会消失:
CASE
WHEN [Variance] = 'M3'
THEN(
(
(SELECT
ABS([Quantity])
FROM #BatchReport
WHERE [Variance] = 'M3')
/
(SELECT
SUM([Quantity])
FROM #BatchReport
WHERE [Quantity] > 0)
) * 100) OVER (PARTITION BY [Batch])
ELSE '0'
END AS [Pct Loss]
类似于我想要得到的样例:
谢谢!
你的表达逻辑基本看不懂。我猜您想要 "M3" 值与总体值之间的某种比率。我最好的猜测是:
(100.0 * SUM(CASE WHEN [Variance] = 'M3' THEN ABS([Quantity]) END) /
SUM(CASE WHEN Quantity > 0 THEN Quantity END ) OVER (PARTITION BY Batch)
) as [Pct Loss]
但是,您的 GROUP BY
非常复杂,如果您的查询符合您的要求,我会感到很惊讶。我认为你应该 post 另一个关于样本数据和预期结果的问题。
谁能告诉我为什么下面的代码无法编译?
我正在尝试计算 'M3' 标记的交易占整个批次的百分比。
我认为如果只有一批就足够容易了,但有时会有多批。
完整代码如下:
SELECT
[Batch],
[DocNum],
[Date],
[Time],
[Location],
[ItemCode],
[Brand],
[Quantity],
CASE
WHEN [Variance] IS NULL
THEN ''
ELSE [Variance]
END AS [Variance],
CASE
WHEN [Comments] IS NULL
THEN ''
ELSE [Comments]
END AS [Comments],
CASE
WHEN [Variance] = 'M3'
THEN(
(
(SELECT
ABS([Quantity])
FROM #BatchReport
WHERE [Variance] = 'M3')
/
(SELECT
SUM([Quantity])
FROM #BatchReport
WHERE [Quantity] > 0)
) * 100) OVER (PARTITION BY [Batch])
ELSE '0'
END AS [Pct Loss],
SUM([Quantity]) OVER (PARTITION BY [Batch]) AS [Difference],
((SUM([Quantity]) - MAX([Quantity])) * 100) / MAX([Quantity]) OVER (PARTITION BY [Batch]) AS [Pct Difference]
FROM #BatchReport
WHERE [DocNum] IS NOT NULL
GROUP BY [Batch], [DocNum], [Date], [Time], [Variance], [Brand], [Comments], [Quantity], [Location], [ItemCode]
ORDER BY [Batch], [Date], [Time]
错误文本:Incorrect syntax near the keyword 'OVER'.
如果我删除这部分,错误就会消失:
CASE
WHEN [Variance] = 'M3'
THEN(
(
(SELECT
ABS([Quantity])
FROM #BatchReport
WHERE [Variance] = 'M3')
/
(SELECT
SUM([Quantity])
FROM #BatchReport
WHERE [Quantity] > 0)
) * 100) OVER (PARTITION BY [Batch])
ELSE '0'
END AS [Pct Loss]
类似于我想要得到的样例:
谢谢!
你的表达逻辑基本看不懂。我猜您想要 "M3" 值与总体值之间的某种比率。我最好的猜测是:
(100.0 * SUM(CASE WHEN [Variance] = 'M3' THEN ABS([Quantity]) END) /
SUM(CASE WHEN Quantity > 0 THEN Quantity END ) OVER (PARTITION BY Batch)
) as [Pct Loss]
但是,您的 GROUP BY
非常复杂,如果您的查询符合您的要求,我会感到很惊讶。我认为你应该 post 另一个关于样本数据和预期结果的问题。