Google BigQuery:TABLE_QUERY和TABLE_DATE_RANGE

时间:2017-04-06 04:41:45

标签: google-bigquery

我有如下所示的大查询表,并且喜欢向标记为<=的表发出查询。

prefix_AAAAAAA_20170320
prefix_AAAAAAA_20170321
prefix_AAAAAAA_20170322 <=
prefix_AAAAAAA_20170323 <=
prefix_AAAAAAA_20170324 <=
prefix_AAAAAAA_20170325
prefix_BBBBBBB_20170320
prefix_BBBBBBB_20170321
prefix_BBBBBBB_20170322 <=
prefix_BBBBBBB_20170323 <=
prefix_BBBBBBB_20170324 <=
prefix_BBBBBBB_20170325
prefix_CCCCCCC_20170320
prefix_CCCCCCC_20170321
prefix_CCCCCCC_20170322
prefix_CCCCCCC_20170323
prefix_CCCCCCC_20170324
prefix_CCCCCCC_20170325

我做了一个查询

SELECT * FROM 
  (TABLE_QUERY(mydataset, 
    'table_id CONTAINS "prefix" AND 
    (table_id CONTAINS "AAAAAA" OR table_id CONTAINS "BBBBBB")' )
   AND
   TABLE_DATE_RANGE(mydataset.prefix, TIMESTAMP('2017-03-22'), TIMESTAMP('2017-03-24')))

我收到了这个错误。

Error: Encountered " "AND" "AND "" at line 5, column 4. Was expecting: ")" ...

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

您不能将TABLE_QUERY和TABLE_DATE_RANGE混合使用完全相同的FROM!

尝试以下内容

   
#legacySQL
SELECT * 
FROM (TABLE_QUERY(mydataset, 'REGEXP_MATCH(table_id, "prefix_[AB]{7}_2017032[234]")'))  

考虑Migrating to BigQuery Standard SQL

在这种情况下,您可以Query Multiple Tables Using a Wildcard Table

请参阅How to Migrate from TABLE_QUERY() to _TABLE_SUFFIX

我认为,在这种情况下,您的查询可能看起来像

#standardSQL
SELECT * 
FROM `mydataset.prefix_*`
WHERE REGEXP_CONTAINS(_TABLE_SUFFIX, '[AB]{7}_2017032[234]')  
  

我无法迁移到标准SQL,因为...
  如果我想在2017-03-29和2017-04-02之间搜索一下,你有没有智能SQL

尝试以下版本

#legacySQL
SELECT * 
FROM (TABLE_QUERY(mydataset, 
    'REGEXP_MATCH(table_id, r"prefix_[AB]{7}_(\d){8}") AND 
    RIGHT(table_id, 8) BETWEEN "20170329" AND "20170402"'))  

当然你可以调整以上使用你需要应用的任何逻辑!