下面的代码创建一个新列“ ZScore”
SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['Card Data']
ORDER BY [ZScore] DESC;
我想做的是仅显示ZScore> = 3的行。我尝试了以下操作,但一切似乎都出错了。
SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['PCard Output']
HAVING (([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ())) > 3
ORDER BY [ZScore] DESC;
仅显示计算出的z得分> = 3的行的正确方法是什么?
答案 0 :(得分:5)
只需使用子查询:
SELECT cd.*
FROM (SELECT [Cardholder Name], [Debit Amount], ([Debit Amount] - AVG([Debit Amount]) OVER ()) / (STDEV([Debit Amount]) OVER ()) as [ZScore]
FROM ['Card Data']
) cd
WHERE ZScore > 3
ORDER BY [ZScore] DESC;