我如何通过SQL中的聚合字段过滤(where子句)(how can i filter (where clause) by an aggregate field in SQL)

编程入门 行业动态 更新时间:2024-10-26 23:34:35
我如何通过SQL中的聚合字段过滤(where子句)(how can i filter (where clause) by an aggregate field in SQL)

我有以下查询:

Select Distinct Product, sum(Deals) as TotalDeals from Metrics m group by Product

我想将查询更改为仅返回TotalDeals = 0的位置。

在查询中使用where子句的最佳方法是什么(而不是在事实之后过滤掉结果集)

I have the following query:

Select Distinct Product, sum(Deals) as TotalDeals from Metrics m group by Product

and I want to change the query to only return where TotalDeals = 0.

What is the best way to have this where clause in the query (instead of filtering out of the result set after the fact)

最满意答案

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 Select Product, TotalDeals FROM ( Select Product, sum(Deals) as TotalDeals from Metrics m group by Product ) Where TotalDeals=0

You don't need distinct as the records are grouped by product

Or

Select Product, sum(Deals) as TotalDeals from Metrics m group by Product having sum(Deals) = 0

更多推荐

本文发布于:2023-08-05 22:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   字段   SQL   filter   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!