2016-05-14 65 views
0
select distinct hash, title, count(*) as c 
from pastes 
where hash is not null 
group by hash, title 
order by c desc; 

我可以根据查询中实时定义的'c'列对查询结果进行排序。如何在我的WHERE子句中使用聚合度量

但我也想加入C到WHERE子句:

select distinct hash, title, count(*) as c 
from pastes 
where c > 10 and hash is not null 
group by hash, title 
order by c desc; 

ERROR: column "c" does not exist 
LINE 1: ...inct hash, title, count(*) as c from pastes where c > 10 and... 
                  ^

什么是指定这样的查询的正确方法?

回答

2

条件使用having

select hash, title, count(*) as c 
from pastes 
where hash is not null 
group by hash, title 
having count(*) > 10 
order by c desc; 
0

你可以只把它包装成一个子查询:

select q.* from (
select hash, title, count(*) as c 
from pastes where hash is not null 
group by hash, title 
) q 
where q.c > 10 
order by q.c desc; 

你可以跳过子查询和使用具有相对于哪里,但我看你还需要按C,因此子查询。在聚集

+0

的HAVING节似乎与C语言对以完成工作。 – user3556757