从查询中获取天数间隔

Get days interval from query

我有一个查询,其中 returns 使用 _TABLE_SUFFIX 选项的特定日期的行。

我的where子句原来是这样的:

WHERE _TABLE_SUFFIX  
BETWEEN FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY)) AND
FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))

哪个有效,我怎么知道将要处理的数据是 139.5MB:

问题是我希望从 table 中动态加载“2”参数,如下所示:

WHERE _TABLE_SUFFIX  
  BETWEEN FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 
  (select value from `path.to.days_interval_setting`) DAY)) AND
          FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))

现在,虽然值相同 (2) 现在的估计表明它将处理 12.3 GB:

事实上它似乎确实在做,查询需要更多时间 运行 但返回的数据来自正确的天数。

我也尝试加入 table 而不是使用子查询,结果相同。

有人知道我该如何解决这个问题吗?

好的我找到了答案here

Filters on _TABLE_SUFFIX that include subqueries cannot be used to limit the number of tables scanned for a wildcard table. For example, the following query does not limit the tables scanned for the wildcard table bigquery-public-data.noaa_gsod.gsod19*

To limit it based on the condition involving the subquery, you can perform two separate queries.

First query:

#standardSQL
# Get the list of tables that match the required table name prefixes
SELECT SUBSTR(MAX(table_id), LENGTH('gsod19') + 1)
      FROM `bigquery-public-data.noaa_gsod.__TABLES_SUMMARY__`
      WHERE table_id LIKE 'gsod194%'

Second query:

#standardSQL
# Construct the second query based on the values from the first query
SELECT
  ROUND((max-32)*5/9,1) celsius
FROM
  `bigquery-public-data.noaa_gsod.gsod19*`
WHERE _TABLE_SUFFIX = '49'