首页 > 解决方案 > Amazon Athena SQL 查询未产生所需结果 - 占总数的百分比

问题描述

我在 Athena 中试过这个,但我相信它也是一个正确的 SQL 查询。

我有一个带有channel列的表,我想得到“每月频道的份额等于频道总数中的 1”。所以我考虑过使用条件语句,当它为 1 时放置 1,否则为 0,然后将其相加,这就像计算 1 的出现次数除以一个月的总数。但它只是给了我零而不是实际的份额。查询有什么问题?

select
  month,
  count(), 
  sum(case when channel = 1 then 1 else 0 end) / count()
from table
where bla bla 
group by 1 order by 1;

标签: sqlconditional-statementsprestoamazon-athena

解决方案


您的问题可能是整数除法。这里有两种方法:

Select month , count(*), sum(case when channel = 1 then 1.0 else 0 end) / count(*)
from table
where bla bla
group by 1
order by 1;

Select month , count(), avg(case when channel = 1 then 1.0 else 0 end)
from table
where bla bla
group by 1
order by 1;

请注意,这avg()是进行此计算的一种更简单的方法。


推荐阅读