简体   繁体   English

过滤器是什么意思?

[英]What does filter mean?

select driverid, count(*) 
from f1db.results
    where position is  null
group by driverid order by driverid 

My thought: first find out all the records that position is null then do the aggregate function.我的想法:首先找出所有位置为空的记录,然后进行聚合函数。

select driverid, count(*) filter (where position is null) as outs
   from f1db.results 
group by driverid order by driverid

First time, I meet with filter clause, not sure what does it mean?第一次遇到 filter 子句,不知道是什么意思? Two code block results are different.两个代码块结果不同。 Already googled, seems don't have many tutorials about the FILTER.已经谷歌搜索,似乎没有很多关于过滤器的教程。 Kindly share some links.请分享一些链接。

A more helpful term to search for is aggregate filters , since FILTER (WHERE) is only used with aggregates.一个更有用的搜索术语是aggregate filters ,因为FILTER (WHERE)仅用于聚合。

Filter clauses are an additional filter that is applied only to that aggregate and nowhere else.过滤器子句是一个额外的过滤器,它只适用于该聚合而不适用于其他任何地方。 That means it doesn't affect the rest of the columns!这意味着它不会影响其余的列!

You can use it to get a subset of the data, like for example, to get the percentage of cats in a pet shop:您可以使用它来获取数据的子集,例如,获取宠物店中猫的百分比:

SELECT shop_id,
       CAST(COUNT(*) FILTER (WHERE species = 'cat')
            AS DOUBLE PRECISION) / COUNT(*) as "percentage"
FROM animals
GROUP BY shop_id

For more information, see the docs有关更多信息,请参阅文档

FILTER ehm... filters the records which should be aggregated - in your case, your aggregation counts all position s that are NULL . FILTER ehm... 过滤应聚合的记录 - 在您的情况下,您的聚合计算所有position NULL

Eg you have 10 records:例如,您有 10 条记录:

SELECT COUNT(*)

would return 10.将返回 10。

If 2 of the records have position = NULL如果 2 条记录的position = NULL

than

SELECT COUNT(*) FILTER (WHERE position IS NULL)

would return 2.将返回 2。

What you want to achieve is to count all non- NULL records:您想要实现的是计算所有非NULL记录:

SELECT COUNT(*) FILTER (WHERE position IS NOT NULL)

In the example, it returns 8.在示例中,它返回 8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM