2014-09-26 85 views
0

如何BY子句結果如何計算在GROUPBY條款計數計數值的數量

select count(*) 
from 
( 
    select first_name,count(first_name) 
    from actor 
    group by first_name 
    having count(first_name) in (2,4) 
); 
+2

你能分享一些樣本數據和你試圖實現的結果嗎? – Mureinik 2014-09-26 13:21:10

+0

這不是你想要的結果嗎?這會給你不同的名字的數量 – Hogan 2014-09-26 13:21:19

+1

你錯過了你的派生表的別名,即最後一行應該是')t1;' – FuzzyTree 2014-09-26 13:30:12

回答

1

你缺少派生表的別名按組數計數的結果:

select count(*) 
from 
( 
    select first_name,count(first_name) 
    from actor 
    group by first_name 
    having count(first_name) in (2,4) 
) as t ;    
    --^ 
    --|------------------------ alias "t" 

的查詢也被簡化了一下:

select count(*) 
from 
(      -- the columns can be skipped, 
    select 1    -- as they are not needed in the upper level 
    from actor 
    group by first_name 
    having count(*) in (2,4) 
) as t ; 

或迷惑性:

select distinct 
    count(case when count(*) in (2,4) then 1 end) over() 
from actor 
group by first_name ;