在 Impala 中划分不同类别的行数

Dividing counts of rows with different categories in Impala

您好,我有 table 这样的情况,我想构建视图以获取每种类型的总和(计数)率。
对于每种类型,我想将“cat”中具有 NULL 的计数总和与“cat”中具有非空值的计数总和相除

这是我的查询,结果不正确(我想是因为我需要先对计数求和,但我很难这样做)

select case   
    When protocol = 61002 AND type= 3 THEN   "S11 Success Rate" 
    When protocol= 61002 AND type = 4 THEN   "S11 Bearer Success Rate"  
end as name,  
max(case when cat is null then count end ) / nullif(max(case when cat is not null then count end),0) as result  
from table

使用这个 table:

类型 3 的预期结果是 result=(63+15456) / (51609+18127)=0.2225

我觉得这就是您要找的:

select 
case
    When protocol = 61002 AND type = 3 THEN "S11 Success Rate" 
    When protocol = 61002 AND type = 4 THEN "S11 Bearer Success Rate"
end as name,
sum(case when cat is null then count else 0 end)/sum(case when cat is null then 0 else count end)
from table
group by name

这会产生以下结果:

S11 Success Rate 0.22253929104049558

S11 Bearer Success Rate 0.90956749672346004

分组确保您可以按类型聚合。 当大小写不正确时,case 语句会将值设置为 0。这会产生您预期的正确总和。