我有以下查询:
Select Distinct Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
我希望将查询更改为仅返回TotalDeals = 0的位置。
在查询中使用where子句的最佳方法是什么(而不是在事实之后过滤掉结果集)
答案 0 :(得分:6)
Select Product, TotalDeals
FROM
(
Select Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
)
Where TotalDeals=0
您不需要区分,因为记录按产品分组
或者
Select Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
having sum(Deals) = 0
答案 1 :(得分:1)
尝试使用HAVING
:
Select Distinct Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
HAVING sum(Deals) = 0
答案 2 :(得分:1)
HAVING
是过滤聚合字段的子句
...
group by Product
HAVING sum(Deals) = 0
答案 3 :(得分:1)
Select Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
having sum(Deals)=0
答案 4 :(得分:0)
要使其在MySQL数据库中工作,并且不接收错误代码1248:每个派生表必须拥有自己的别名,
此查询:
Select Product, TotalDeals
FROM
(
Select Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
)
Where TotalDeals=0
必须更新以在闭括号后包含 AS 别名,如下所示:
Select Product, TotalDeals
FROM
(
Select Product, sum(Deals) as TotalDeals
from Metrics m
group by Product
) AS t1
Where TotalDeals=0