BigQuery的标准SQL中的日期和时间函数

时间:2016-04-28 22:25:41

标签: google-bigquery

我试图在BigQuery的新标准SQL功能上使用我的一个MSSQL查询,因为它需要特殊的JOIN条件。但是,该引用不包含BigQuery标准SQL中日期时间函数的任何解释。

到目前为止,我设法转换了我的DATE_ADD语句 来自: DATE_ADD('2015-01-01', - 13,'MONTH') 至: DATE_ADD('2015-01-01',INTERVAL -13 MONTH)

我还将YEAR(x)转换为EXTRACT(YEAR FROM x)。两者似乎都是类似MySQL的声明。

最后我注意到大多数日期函数的输出必须先用TIMESTAMP转换才能比较它们。

但是现在我的查询终于要运行了,我得到一个“内部错误”。谁知道什么可能是错的?这是查询:

SELECT
  M,
  Bought12MBeforeM,
  Bought12MBeforeAndInM,
  (Bought12MBeforeAndInM * 1.0) / (Bought12MBeforeAndInM + Bought12MBeforeM) AS RepurchaseRate
FROM (
  SELECT
    M,
    COUNT(DISTINCT CASE WHEN MaxM < TIMESTAMP(M) THEN user_id ELSE NULL END) AS Bought12MBeforeM,
    COUNT(DISTINCT CASE WHEN MaxM >= TIMESTAMP(M)
      AND MinM < TIMESTAMP(M) THEN user_id ELSE NULL END) AS Bought12MBeforeAndInM
  FROM (
    SELECT
      M,
      user_id,
      MAX(cart_creation_date) AS MaxM,
      MIN(cart_creation_date) AS MinM
    FROM (
      SELECT
        user_id,
        cart_creation_date
      FROM
        `vex-eu-cloud-sql-001.work_user.flat_orders`
      WHERE
        cart_creation_date >= TIMESTAMP(DATE_ADD('2015-01-01', INTERVAL -13 MONTH))
      GROUP BY
        user_id,
        cart_creation_date ) UniqueCarts
    INNER JOIN (
      SELECT
        DATE(Day) AS M
      FROM
        `vex-custom.wouter.days_2007_2020`
      WHERE
        EXTRACT(YEAR
        FROM
          Day) >= 2015
        AND EXTRACT(DAY
        FROM
          Day) = 1
        AND Day < TIMESTAMP(CURRENT_DATE()) ) Months
    ON
      cart_creation_date > TIMESTAMP(DATE_ADD(M, INTERVAL -13 MONTH))
      AND cart_creation_date < TIMESTAMP(DATE_ADD(M, INTERVAL 1 MONTH))
    GROUP BY
      M,
      user_id ) BoughtInM
  GROUP BY
    M ) Results

1 个答案:

答案 0 :(得分:1)

错误是由于在标准SQL的WHERE子句中处理TIMESTAMP的错误。目前的解决方法是用UNIX_MICROS函数包装它以从TIMESTAMP转换为INT64,然后查询工作:

SELECT
  M,
  Bought12MBeforeM,
  Bought12MBeforeAndInM,
  (Bought12MBeforeAndInM * 1.0) / (Bought12MBeforeAndInM + Bought12MBeforeM) AS RepurchaseRate
FROM (
  SELECT
    M,
    COUNT(DISTINCT CASE WHEN MaxM < TIMESTAMP(M) THEN user_id ELSE NULL END) AS Bought12MBeforeM,
    COUNT(DISTINCT CASE WHEN MaxM >= TIMESTAMP(M)
      AND MinM < TIMESTAMP(M) THEN user_id ELSE NULL END) AS Bought12MBeforeAndInM
  FROM (
    SELECT
      M,
      user_id,
      MAX(cart_creation_date) AS MaxM,
      MIN(cart_creation_date) AS MinM
    FROM (
      SELECT
        user_id,
        cart_creation_date
      FROM
        `vex-eu-cloud-sql-001.work_user.flat_orders`
      WHERE
        UNIX_MICROS(cart_creation_date) >= UNIX_MICROS(TIMESTAMP(DATE_ADD('2015-01-01', INTERVAL -13 MONTH)))
      GROUP BY
        user_id,
        cart_creation_date ) UniqueCarts
    INNER JOIN (
      SELECT
        DATE(Day) AS M
      FROM
        `vex-custom.wouter.days_2007_2020`
      WHERE
        EXTRACT(YEAR FROM Day) >= 2015
        AND EXTRACT(DAY FROM Day) = 1
        AND UNIX_MICROS(Day) < UNIX_MICROS(TIMESTAMP(CURRENT_DATE())) ) Months
    ON
      cart_creation_date > TIMESTAMP(DATE_ADD(M, INTERVAL -13 MONTH))
      AND cart_creation_date < TIMESTAMP(DATE_ADD(M, INTERVAL 1 MONTH))
    GROUP BY
      M,
      user_id ) BoughtInM
  GROUP BY
    M ) Results