首页 > 解决方案 > 您可以在窗口函数中包含除法吗(红移)

问题描述

我正在寻找一个顾问数据集,并希望使用窗口函数来计算每个顾问的比率。我想知道顾问在打电话给客户时做了多少销售

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;

标签: sqlwindowaggregateamazon-redshiftaggregate-functions

解决方案


您需要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

推荐阅读