如何解决您可以在窗口函数中包含除法吗红移
我正在查找顾问数据集,并希望使用窗口函数来计算每个顾问的比率。 我想知道顾问在给客户打电话时做了多少销售
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
现在我怀疑在这种情况下我是否可以使用窗口函数。我得到的错误是:数据库报告语法错误:亚马逊无效操作:语法错误在或接近“结束”位置:3191;
解决方法
我想知道顾问在给客户打电话时做了多少销售
我有点困惑窗口函数的作用。这听起来像条件聚合:
select "consultant",sum(case when sales = 1 then 1 else 0 end) as num_sales,sum(case when sales = 1 and "call to" = 'customer' then 1 else 0 end) as num_sales_with_call,sum(case when sales = 1 then 1.0 else 0 end) / sum(case when "call to" = 'customer' then 1 end) as sales_to_call_ratio
from consultant
group by 1;
,
需要对两个解析函数分别使用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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。