2014-09-26 94 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 ;