Skip to content
Roman Prokashev.

This is an archive of the Last Click City blog, which was published at lastclick.city from 2019 to 2024.

2 min readBy Sergey Matrosov

BigQuery: totals by day, totals by date range in one table.

Let’s say that you want to know the conversion amount in a specific date range for streaming in BigQuery: the total amount for each day and the total for all days as well. You could do this by making either two queries resulting in two tables, or one query resulting in one table. The first solution is not particularly user-friendly (you have to click from one tab to another etc.), but it is very quick to carry out. The second solution sounds pretty easy too, although it could involve a slight challenge; however, in terms of output, one table is produced.

Our conversions include ‘Registrations’, thus our streaming table looks like this:

DateEvent
2019-01-01Registration
2019-01-01Activation
2019-01-01Registration
......
2019-01-02Registration
......

And so on.

Obtaining data only by day is really that easy:

SELECT
  date,
  COUNT(event) AS Conversions
FROM
  `your-project.your_dataset.table`
WHERE
  event = 'Registration'
  AND date BETWEEN ‘2019-01-01’
  AND ‘2019-01-05’
GROUP BY
  date
DateConversions
2019-01-0110
2019-01-025
2019-01-036
2019-01-041
2019-01-052

Then, for the grand total (10 + 5 + 6 + 1 + 2 = 24 in our case), you might think that it’s all about one line of query in SELECT, but it’s not. Using “Group By” will only count events by each day.

To achieve your goal, you need to use “CROSS JOIN” with this query:

SELECT
  COUNT(event) AS Total,
FROM
  `your-project.your_dataset.table`
WHERE
  event = 'Registration'
  AND date BETWEEN ‘2019-01-01’
  AND ‘2019-01-05’

So, the full query will be:

SELECT
  t1.date AS Date,
  t1.Conversions AS Conversions,
  t2.Total AS Grand_Total
FROM (
  SELECT
  date,
  COUNT(event) AS Conversions
FROM
  `your-project.your_dataset.table`
WHERE
  event = 'Registration'
  AND date BETWEEN ‘2019-01-01’
  AND ‘2019-01-05’) AS t1
CROSS JOIN (
SELECT
  COUNT(event) AS Total,
FROM
  `your-project.your_dataset.table`
WHERE
  event = 'Registration'
  AND date BETWEEN ‘2019-01-01’
  AND ‘2019-01-05’) AS t2
ORDER BY
  Date ASC

And the result will be as expected:

DateConversionsGrand_Total
2019-01-011024
2019-01-02524
2019-01-03624
2019-01-04124
2019-01-05224

Indeed, as a result, you will get a duplicate of the total in each row. However, the result clearer and more convenient, especially if it is used only in BigQuery’s interface.