[英]How to use sum in where clause in SQL?
I want to execute this query:我想执行这个查询:
Select Environment, SUM(Scores) as `Scores`,SUM(Clicks) as `Clicks`
from Example.table
where Scores > sum(Scores)
group by Environment
But it throws the following error:但它抛出以下错误:
Aggregate function SUM not allowed in WHERE clause at [4:27] [4:27] 的 WHERE 子句中不允许使用聚合函数 SUM
whereas for the case of having: having Scores > sum(Scores)
doesn't allow me to keep Scores
.而对于具有的情况: having Scores > sum(Scores)
不允许我保留Scores
。 What is the right way to implement this?实现这一点的正确方法是什么?
Your query should be like this:你的查询应该是这样的:
Select Environment
,Scores
,SUM(Scores) as `Scores`
,SUM(Clicks) as `Clicks`
from TABLE
group by Environment, Scores
HAVING (Scores > sum(Scores))
You need to include Score and group by that also (even if you don't need it).您还需要包括 Score 和 group by(即使您不需要它)。
You can only use the HAVING clause for comparison:您只能使用 HAVING 子句进行比较:
GROUP BY ...
HAVING Scores > sum(Scores)
So your SQL will be something like:所以你的 SQL 将类似于:
Select Environment, SUM(Scores) as `Scores`,SUM(Clicks) as `Clicks`
from Example.table
group by Environment
HAVING Scores > sum(Scores)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.