BigQuery - 无法加入重复字段

时间:2015-12-23 19:02:12

标签: google-bigquery

我试图创建一个1列的表,每行是两个不同日期之间的新日期。查询工作正常,直到我添加包含子查询的where子句,即。不在(选择....)。如果我执行NOT IN(TIMESTAMP('xyz'))之类的操作,它可以正常工作。

我不断收到错误消息“无法加入重复字段t2.f0__group.SomeDate”

我不知道为什么会这样。我也是BQ的新手,所以如果有更简单的方法,请告诉我。感谢

SELECT SomeDate FROM
(
  SELECT DATE_ADD(Day, i, "DAY") SomeDate
  FROM
  (
    SELECT '2020-01-03' Day
  ) T1
  CROSS JOIN
  (
    SELECT
     POSITION(
       SPLIT(
         RPAD('', DATEDIFF('2020-01-30','2020-01-03') * 2, 'a,'))) i 
    FROM
    (
      SELECT NULL
    )
  ) T2
)
WHERE SomeDate NOT IN (SELECT OtherDate FROM 
  (
    SELECT TIMESTAMP('2020-01-04 00:00:00 UTC') AS OtherDate
  ),
  (
    SELECT TIMESTAMP('2020-01-06 00:00:00 UTC') AS OtherDate
  ),
  (
    SELECT TIMESTAMP('2020-01-08 00:00:00 UTC') AS OtherDate
  )
)

1 个答案:

答案 0 :(得分:1)

我建议使用以下示例从头开始 我认为这正是你想要实现的,可能是微小的调整

SELECT SomeDate
FROM (
  SELECT 
    DATE(DATE_ADD(TIMESTAMP('2020-01-03'), pos - 1, "DAY")) AS SomeDate
  FROM (
     SELECT ROW_NUMBER() OVER() AS pos, *
     FROM (FLATTEN((
       SELECT SPLIT(RPAD('', 1 + DATEDIFF(TIMESTAMP('2020-01-30'), TIMESTAMP('2020-01-03')), '.'),'') AS h
       FROM (SELECT NULL)),h
    ))
  )
) a
LEFT JOIN (
SELECT OtherDate FROM
    (SELECT '2020-01-04' AS OtherDate),
    (SELECT '2020-01-06' AS OtherDate),
    (SELECT '2020-01-08' AS OtherDate)
) b
ON b.OtherDate = a.SomeDate
WHERE b.OtherDate IS NULL