0

用例 - 我正在尝试从数据集中查找客户的每周频率。现在,并非所有客户在所有几周内都发生了“事件”,我需要在“计数”列中用零值填充它们。

我试图使用 PrestoSQL 的序列函数来做到这一点。但是,这需要我从客户的订单本身中获取 max week 的值(我不想硬编码,因为结果将进入 BI 工具,我不想每周手动更新)

with all_orders_2020  as (select customer, cast(date_parse(orderdate, '%Y-%m-%d') as date) as order_date 
from orders 
where orderdate > '2020-01-01' and customer in (select customer from some_customers)),

orders_with_week_number as (select *, week(order_date) as week_number from all_orders_2020),

weekly_count as (select customer, week_number, count(*) as ride_count from orders_with_week_number
where customer = {{some_customer}} group by customer, week_number)

SELECT
    week_number
FROM
    (VALUES
        (SEQUENCE(1,(select max(week_number) from weekly_count)))
    ) AS t1(week_array)
CROSS JOIN
    UNNEST(week_array) AS t2(week_number)

Presto 抱怨这句话——

Unexpected subquery expression in logical plan: (SELECT "max"(week_number)
FROM
  weekly_count
)

任何线索如何做到这一点?

4

0 回答 0