我正在调查移植一些包含窗口的bigquery legacy sql,
count(distinct brand_id) over (partition by user_id order by order_placed_at range between 7 * 24 * 60 * 60 * 1000000 PRECEDING AND 1 PRECEDING) as last_7_day_buyer_brands
到标准SQL ....但是我得到了这个错误....
Window ORDER BY is not allowed if DISTINCT is specified
作为参考,我尝试过APPROX_COUNT_DISTINCT
功能而没有运气。
除了写下子征服和群体外,有没有更好的方法来工作?
其他大多数查询仅移植到标准SQL,只有较小的更改。
每个文档
根据条款要求:
分区作者:可选。
订购者:可选。如果存在不同的情况。
window_frame_clause:可选。如果存在不同的情况,则禁止。
注意:上面是我"突出显示的",而不是在文档中
您不仅可以看到ORDER BY
,而且在使用DISTINCT
时也不允许RANGE BETWEEN
我认为,子查询是要走的方式。
如果您需要此方向,请使用以下简单示例
#standardSQL
SELECT
user_id,
order_placed_at,
brand_id,
(SELECT COUNT(DISTINCT brand)
FROM UNNEST(last_7_day_buyer_brands_with_dups) AS brand
) AS last_7_day_buyer_brands
FROM (
SELECT
user_id,
order_placed_at,
brand_id,
ARRAY_AGG(brand_id) OVER(
PARTITION BY user_id ORDER BY order_placed_at
RANGE BETWEEN 7 * 24 * 60 * 60 * 1000000 PRECEDING AND 1 PRECEDING
) AS last_7_day_buyer_brands_with_dups
FROM yourTable
)