使用FILTER子句时出现奇怪的行为

时间:2017-04-04 12:36:46

标签: sql performance postgresql

我有两个问题

SELECT COUNT(DISTINCT keyword_id),
  date_trunc('month', rank_date) AS date
  FROM keyword_ranks, keywords
  WHERE keywords.deleted_at IS NULL
  AND keywords.id=keyword_ranks.keyword_id 
  AND keywords.business_id=27 GROUP BY date_trunc('month', rank_date);

结果集是

enter image description here

现在我添加了一些FILTER条款,例如

SELECT COUNT(DISTINCT keyword_id) FILTER (WHERE rank>50 OR rank is null) AS "50+",
  COUNT(DISTINCT keyword_id) FILTER (WHERE rank BETWEEN 21 AND 50) AS "21-50",
  COUNT(DISTINCT keyword_id) FILTER (WHERE rank BETWEEN 11 AND 20) AS "11-20",
  COUNT(DISTINCT keyword_id) FILTER (WHERE rank BETWEEN 4 AND 10) AS "4-10",
  COUNT(DISTINCT keyword_id) FILTER (WHERE rank BETWEEN 1 AND 3) AS "1-3",
  date_trunc('month', rank_date) AS date
  FROM keyword_ranks, keywords
  WHERE keywords.deleted_at IS NULL
  AND keywords.id=keyword_ranks.keyword_id 
  AND keywords.business_id=27 GROUP BY date_trunc('month', rank_date);

结果集为

enter image description here

从第一个查询结果中可以看出,每个月的不同keyword_id的计数是147.但是为什么使用FILTER子句时的计数加起来超过147?如果这不是获得不同计数的方式,那么如何解决这个问题。请忍受我的新手问题。任何帮助非常感谢

1 个答案:

答案 0 :(得分:1)

ids在等级内是不同的,但不在总数内。检查此示例:

with v (id, rank) as (values (1,1),(1,10))
select
    count(distinct id) as total,
    count(distinct id) filter (where rank < 10) as "< 10",
    count(distinct id) filter (where rank >= 10) as ">= 10"
from v
;
 total | < 10 | >= 10 
-------+------+-------
     1 |    1 |     1

使得排名总和与总数匹配的唯一方法是不使用distinct。

with v (id, rank) as (values (1,1),(1,10))
select
    count(id) as total,
    count(id) filter (where rank < 10) as "< 10",
    count(id) filter (where rank >= 10) as ">= 10"
from v
;
 total | < 10 | >= 10 
-------+------+-------
     2 |    1 |     1