你能在 window 函数中包含一个除法吗(红移)

Can you include a division in a window function (redshift)

我正在查看顾问数据集,并希望使用 window 函数来计算每位顾问的比率。 我想知道顾问在给客户打电话时做了多少销售

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

Table 我正在使用:

现在我怀疑我能否在这种情况下使用 window 函数。我得到的错误是:数据库报告了一个语法错误:Amazon 无效操作:“over”位置或附近的语法错误位置: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

I want to know how many sales the consultant made, when they called a customer

我对 window 函数的用处感到有点困惑。这听起来像是条件聚合:

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;