sql - 您可以在窗口函数中包含除法吗(红移)
问题描述
我正在寻找一个顾问数据集,并希望使用窗口函数来计算每个顾问的比率。我想知道顾问在打电话给客户时做了多少销售
select
"consultant", "country",
(count(case when "sales"=1 then "call id" end) / count(case when "call to"='customer' then "call id" end)
over (partition by "consultant" order by "consultant") as "Sales Ratio"
from consultant
group by 1,2
现在我怀疑在这种情况下我可以使用窗口函数。我得到的错误是:数据库报告了一个语法错误:Amazon Invalid operation: syntax error at or near "over" Position: 3191;
解决方案
您需要OVER
对两个解析函数分别使用该子句,请确保除数大于 0 或使用适当的条件避免被零除。
select "consultant",
"country",
count(case when "sales"=1 then 1 end) over (partition by "consultant")
/ count(case when "call to"='customer' then 1 end) over (partition by "consultant") as "Sales Ratio"
from consultant
group by 1,2
推荐阅读
- python - 更新和合并熊猫数据框中的列
- xamarin.forms - Xamarin Forms - UWP - 通过应用中心部署的 VSTS 任务 - Yaml
- javascript - php mysql_fetch_object 无限转
- python - Python将元组转换为字符串
- http - 在golang中强制关闭http连接
- facebook - 使用 laravel localhost 时出错:8000 facebook 无法加载 URL appdomain facebook 登录
- haskell - 从包含名称和 id 的元组列表中,我想要包含名称的元组和获得类似 id 的名称列表
- sql - 大于和小于的表连接
- python - 有效替换 pandas df 中的值
- python - 在python中用多个列表计算欧几里得距离