You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

连接BigQuery的Google Sheet表透视后自关联免建额外表实现咨询

方案1:使用窗口函数(更高效,无需关联)

该方案直接利用BigQuery原生窗口函数实现需求,仅需扫描一次原表,不需要额外关联操作,也不会生成实体表,执行效率更高:

SELECT
  DATE(datecreated) as SO_Date,
  customerid as Customer_ID,
  totalprice as SO_Total,
  -- 按客户ID分组计算该客户所有订单的最小年月,即为同期群标签
  MIN(FORMAT_DATE("%Y%m", datecreated)) OVER (PARTITION BY customerid) as Cohort_Date
FROM `fishbowl_raw_data.fishbowl_so`

说明:FORMAT_DATE("%Y%m", datecreated) 等价于你原有拼接年份、补零月份的逻辑,写法更简洁不易出错。OVER (PARTITION BY customerid) 会将数据按客户ID分组,MIN函数针对每个分组单独计算最小值,直接将结果追加到对应客户的每一行订单记录中。


方案2:使用CTE公用表表达式(适配原有逻辑调整)

如果你希望保留先聚合客户同期群再关联的原有逻辑,可以用WITH子句定义临时结果集,该结果集仅在当前查询生效,不会实际写入到你的BigQuery数据集:

WITH Cohort_List AS (
    SELECT
      MIN(FORMAT_DATE("%Y%m", datecreated)) as Cohort_Date,
      customerid as CL_Customer_ID
    FROM `fishbowl_raw_data.fishbowl_so`
    GROUP BY customerid
)
SELECT
  DATE(datecreated) as SO_Date,
  customerid as Customer_ID,
  totalprice as SO_Total,
  Cohort_List.Cohort_Date
FROM `fishbowl_raw_data.fishbowl_so`
JOIN Cohort_List
ON `fishbowl_raw_data.fishbowl_so`.`customerid`=Cohort_List.CL_Customer_ID

内容的提问来源于stack exchange,提问作者Jason Krueger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 16:15:08