sql - 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;
解决方案
您的问题可能是整数除法。这里有两种方法:
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()
是进行此计算的一种更简单的方法。
推荐阅读
- javascript - 有没有办法用 puppeteer 截取屏幕截图并自动显示在 html 页面上,而不是保存到我的电脑上?
- linux - 使用 Bash 从简单的 linux 脚本编辑变量
- go - 在节奏工作流的循环内调用相同的活动
- pytorch - Pytorch:将张量移动到 GPU 时,内存会发生什么变化?
- vba - Word VBA:扫描文档中的字符串,将其复制到列表中,然后移至下一个匹配项
- python - 如何在 Django 中转换字符串中的列表值?
- c++ - 对和花括号检查
- javascript - 尝试使用 Javascript 在 html 上制作时钟,但我的脚本无法正常工作。时钟实际上并没有运行
- excel - MS Access 2016 中 Excel 工作簿的无链接路径
- confluence - Comala/Confluence - 首次应用 Worfklow 时触发