BigQuery - 每个表运行查询

时间:2015-09-30 16:27:56

标签: sql google-bigquery

Big Query允许您使用table_query通过时间戳分隔表。这是我们正在使用的示例查询:

SELECT
  COUNT(*) as num
FROM (
  SELECT
    dsn,
    SUM(hours_used) AS total_hours
  FROM
    [device_use.day20150901] // Now - 1 day
  WHERE
    nest_state > 5
  GROUP BY
    dsn )
WHERE
  total_hours > 1

现在我们想要做的是运行相同的查询,但是基于PER TABLE DAY。 IE,[device_use.day20150901][device_use.day20150902][device_use.day20150903]

这是可能的,还是我必须创建一个单独的表来执行此单个查询。

谢谢!

1 个答案:

答案 0 :(得分:1)

只是在原始查询中添加了一些内容,假设时间戳字段存在于相应的表日内

SELECT
  day,                                          -- added
  COUNT(*) as num
FROM (
  SELECT
    DATE(your_timestamp_field) as day,          -- added
    dsn,
    SUM(hours_used) AS total_hours
  FROM
     (TABLE_DATE_RANGE(device_use.day,          -- added
                    TIMESTAMP('2015-09-01'),    -- added
                    TIMESTAMP('2015-09-29')))   -- added
  WHERE
    nest_state > 5
  GROUP BY
    1,                                          -- added
    2 )
WHERE
  total_hours > 1