Bigquery Queries

Use decorator and extract data for specific date adjusted for local time

SELECT
site,
event_type,
count(*) as total_events,
EXACT_COUNT_DISTINCT(event_message_id) as uniq_events
FROM
[email.sendgrid_events@1471590000000-]
WHERE
event_provider = 'ongage'
AND DATE(DATE_ADD(SEC_TO_TIMESTAMP(time_unix), -7, 'HOUR')) = '2016-08-21'
AND site = 'careeralerts.com'
GROUP BY
site, event_type
ORDER BY
site, event_type

Generating date as local time using moment.js

const moment = require('moment-timezone');

// will be -420 minutes which is 7 hours
var timezoneOffsetMinutes = moment.tz.zone('America/Los_Angeles').parse(moment()) * -1;

SELECT
siteId as site,
type as campaignType,
DATE(DATE_ADD(e.ts, timezoneOffsetMinutes, "minute")) as aggDate ...

Get total counts grouped by the hour adjusted for PST time for the past 10 days

SELECT hour(ts)-7,count(*)
FROM [project:DataSet.SomeTable@-864000000-]
WHERE
  platform='ecapi'
GROUP BY 1
ORDER BY 1

get total counts per minute in past hour

SELECT hour(ts), minute(ts), platform, count(*)
FROM [project:DataSet.SomeTable@-3600000-]
WHERE platform='ecapi'
GROUP BY 1,2,3
ORDER BY 1 DESC, 2 DESC,3

totals in past 7 hours per hour

SELECT hour(ts), platform, count(*)
FROM [rm-data:EmailMetricsCollection.Email@-25200000-]
WHERE platform='ecapi'
GROUP BY 1,2
ORDER BY 1 DESC, 2 DESC

last 10 min

SELECT hour(ts), minute(ts), count(*)
FROM [project:DataSet.Table@-600000-]
WHERE platform='ecapi'
GROUP BY 1,2
ORDER BY 1 DESC, 2 DESC

getting count from range of log tables

SELECT count(*) FROM [project:dataSet.table_20190319],[project:dataSet.table_20190320] where resource.labels.namespace_id = 'prod'
and textPayload contains('Dynamic')

excluding fields in group by and select

SELECT date(ts) date, origin, destination,
  --source,  // exclude field from display
  count(*)
FROM [project:dataset.table]
WHERE (_PARTITIONTIME >= "2019-07-25 00:00:00" OR _PARTITIONTIME IS NULL)
GROUP BY 1,2,3--,4 // Will only group by first 3 columns. 4th is ignored
ORDER BY 1 DESC,2,3--,4 // Will only order by first 3 columns. 4th is ignored


Comments

Popular posts from this blog

Copy partitioned table schema to another table

Using moment.js

Changing BigQuery table field names