簡體   English   中英

PARTITION BY 表達式引用 cust.cif 在 [23:60] 處既不分組也不聚合

[英]PARTITION BY expression references cust.cif which is neither grouped nor aggregated at [23:60]

我不斷收到錯誤:PARTITION BY 表達式引用 cust.cif 既不分組也不聚合 [23:60]

有誰知道問題是什么?

詢問:

WITH friend AS (
  SELECT ID
          , lpad(cast(ID as string), 10, '0') AS customer
  FROM `data-sandbox.WorksNew.friendversary`
),cust AS (
  SELECT customer_id,
        cif,
        customer_start_date
  FROM `data-production.dashboard_views.customer_registration`
),cbal AS (
  SELECT customer_id
          , total_balance
          , full_date
          , balance_tier
  FROM `data-production.data_analytics.customer_record`
  WHERE full_date = "2022-07-22"
  GROUP BY 1,2,3,4
)
SELECT friend.customer
        , SUM(COALESCE(cbal.total_balance, 0)) AS total_balance
        , full_date
        , balance_tier
        , dense_rank() OVER (PARTITION BY friend.customer, cust.cif ORDER BY cbal.full_date DESC) rn_desc
FROM friend
LEFT JOIN cbal
  ON friend.customer = cbal.customer_id
LEFT JOIN cust
  ON friend.customer = cust.customer_id
GROUP BY 1,3,4

問題是因為每一列都必須在 group by 中,包括在 window function 中使用的列,而現在 cust.cif 不包括在內。 根據您的需求,也許您可以將其包含在 group by 中,並且仍然可以得到您想要的。

Another solution that I normally use when I want to combine aggregate and window functions is just to use a window function for my aggregate function, and then you don't have to worry how the group by is affecting your results.

SELECT friend.customer
        , SUM(COALESCE(cbal.total_balance, 0)) over (PARTITION BY friend.customer, full_date, balance_tier) AS total_balance
        , full_date
        , balance_tier
        , dense_rank() OVER (PARTITION BY friend.customer, cust.cif ORDER BY cbal.full_date DESC) rn_desc
FROM friend
LEFT JOIN cbal
  ON friend.customer = cbal.customer_id
LEFT JOIN cust
  ON friend.customer = cust.customer_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM