SQL语言 - 有两种方法可以写平均值吗?

时间:2016-04-15 05:46:32

标签: sql

问题是"列出所有付款大于平均付款额的两倍"

SELECT AVG(amount)
FROM Payments
WHERE amount > AVG(amount)*2;

3 个答案:

答案 0 :(得分:4)

您需要子查询来计算总体平均金额:

SELECT * 
FROM Payments 
WHERE amount > (SELECT AVG(amount)*2 FROM Payments)

答案 1 :(得分:0)

您可以将平均值放在变量

Declare @average int
Select @average = ...
Select * from ... Where x>= @average

答案 2 :(得分:0)

此查询可能对您有所帮助:

SELECT * FROM `Payments` WHERE amount > ((select avg(amount) from `Payments`)*2)