我需要选择多个列作为LEAD语句的一部分。看起来效率真的很低,将所需的排序和分区数量增加了三倍->

SELECT
    field,
    field2,
    field3,
    LEAD(field, 1) OVER (PARTITION BY field ORDER BY field ASC) AS nextField,
    LEAD(field2, 1) OVER (PARTITION BY field ORDER BY field ASC) AS nextField2,
    LEAD(field3, 1) OVER (PARTITION BY field ORDER BY field ASC) AS nextField3,
FROM dataset.table
  • 是否有更好的方法可以做到这一点?
  • BigQuery是否在查询运行时对此进行优化以使其高效?
  • 最佳答案

    有两点可以补充米哈伊尔的答案:

  • 是的,BigQuery对其进行了优化-如果窗框相同,则只会设置一次,并且会在其上运行多个功能。
  • 没错,一遍又一遍地写同一帧很麻烦,因此我们致力于改进BigQuery SQL方言以使其更符合标准,并且在不久的将来,您将能够编写
  • 
    SELECT
        field,
        field2,
        field3,
        LEAD(field, 1) OVER w1 AS nextField,
        LEAD(field2, 1) OVER w1 AS nextField2,
        LEAD(field3, 1) OVER w1 AS nextField3,
    FROM dataset.table
    WINDOW w1 AS (PARTITION BY field ORDER BY field ASC)
    

    *不能真正给您确定日期,但是此刻正在内部测试中,因此不应太长。

    关于google-bigquery - 重复使用BigQuery窗口函数分区,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35513133/

    10-12 21:58