Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

results for last 30 days in bigquery

I am trying to build a query for a tableau dashboard that is connected to Google BigQuery. We have tables for each month of data, but I want to present the last 30 days of data at any given time (so it will have to go across multiple tables). The current query I have gives the error "Timestamp literal or explicit conversion to timestamp is required." I've been looking around for some help on how to convert to timestamp but haven't found anything helpful. This is my code.

    SELECT
      DATE(date_time) AS date,
    FROM
      TABLE_QUERY(myTable, "date(concat(left(table_id,4),'-',right(table_id,2),'-','01')) >= '2017-06-01'")
    WHERE 
        DATE(date_time) >= DATE_ADD(day,-30, current_date()) 
        and   DATE(date_time) <= current_date()
    ORDER BY 
        date

Any help would as to how to get it to work will be greatly appreciated.

Note: we are using legacy SQL

like image 568
kshoe94 Avatar asked Jul 24 '17 19:07

kshoe94


2 Answers

For anyone looking to do the same thing with BigQuery Standard SQL, this will return the last 30 days of data based on the current date.

https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date_add

SELECT
  date,
FROM
  `<PROJECT>.<DATASET>.<TABLE>` 
WHERE
  Date >= DATE_ADD(CURRENT_DATE(), INTERVAL -30 DAY);
like image 67
Mitch Avatar answered Oct 11 '22 23:10

Mitch


Using Google's own documentation, it would appear you need to reverse the parameters in the DATE_ADD() function. The function should be called with three parameters as follows:

DATE_ADD(<timestamp>,<interval>,<interval_units>)

with interval units being the kind of time unit you're referencing (day, hour, year etc.)

The reason you're getting the error you are is that Google is trying to convert day into some sort of date format.

Source: Google Query Reference

Quoted Text:

DATE_ADD(timestamp,interval, interval_units)

Adds the specified interval to a TIMESTAMP data type. Possible interval_units values include YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND. If interval is a negative number, the interval is subtracted from the TIMESTAMP data type.

Example:

SELECT DATE_ADD(TIMESTAMP("2012-10-01 02:03:04"), 5, "YEAR");

Returns: 2017-10-01 02:03:04 UTC

SELECT DATE_ADD(TIMESTAMP("2012-10-01 02:03:04"), -5, "YEAR");

Returns: 2007-10-01 02:03:04 UTC

like image 4
Cowthulhu Avatar answered Oct 11 '22 22:10

Cowthulhu