Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLATTEN with TABLE_DATE_RANGE

I have a range of tables in a dataset and need to query all of them while FLATTENing one of the repeated records. For example, a query like the following works fine:

 SELECT
   date,
   customDimensions.value AS customDimension,
   hits.page.pagePath AS pagePath
FROM
   (FLATTEN( [<projectId>:<datasetId>.ga_sessions_20130910] ,
   customDimensions)) 
WHERE
   hits.page.pagePath CONTAINS '/helmets' 
   AND customDimensions.index IN (1,2,3)

However, I am having trouble FLATTENing while using table wildcards. Can someone help me out with the syntax? Is it possible to use FLATTEN with TABLE_DATE_RANGE?

SELECT
   date,
   customDimensions.value AS customDimension,
   hits.page.pagePath AS pagePath
FROM
   (FLATTEN (TABLE_DATE_RANGE ([<project>:<dataset>.ga_sessions_],
   TIMESTAMP('2013-09-10'),
   TIMESTAMP ('2014-06-10'))),
   customDimensions) 
WHERE
   hits.page.pagePath CONTAINS '/helmets' 
   AND customDimensions.index IN (1,2,3)

Thanks, Shayan

like image 523
Shayan Masood Avatar asked Jun 12 '14 16:06

Shayan Masood


People also ask

What is legacysql?

Legacy SQL allows reserved keywords in some places that Google Standard SQL does not. For example, the following query fails due to a Syntax error using standard SQL: #standardSQL SELECT COUNT(*) AS rows FROM `bigquery-public-data.

How do you select top 10 rows in BigQuery?

Instead, use the handy preview feature in BigQuery. Just click on the table name, and then click on the Preview tab to see the top 100 rows.

What SQL dialect does BigQuery use?

BigQuery supports the Google Standard SQL dialect, but a legacy SQL dialect is also available. If you are new to BigQuery, you should use Google Standard SQL as it supports the broadest range of functionality. For example, features such as DDL and DML statements are only supported using Google Standard SQL.


1 Answers

According to reference manual, the FLATTEN syntax is:

...(FLATTEN ([project_name:]datasetId.tableId, flattenField))..
...(FLATTEN (subselect_clause, flattenField))..

Based on this, I would try putting a subselect clause within the FLATTEN statement like so:

SELECT
date,
customDimensions.value AS customDimension,
hits.page.pagePath AS pagePath
FROM
FLATTEN(
(SELECT date, customDimensions, hits FROM TABLE_DATE_RANGE ([<project>:<dataset>.ga_sessions_],
TIMESTAMP('2013-09-10'),TIMESTAMP ('2014-06-10')))
, customDimensions)
WHERE
hits.page.pagePath CONTAINS '/helmets' 
AND customDimensions.index IN (1,2,3)
like image 193
David M Smith Avatar answered Oct 01 '22 06:10

David M Smith