2016-05-14 66 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