1
votes

I'm new to BQ/SQL.

I'm using Google Analytics dataset to pull a COUNT in the same query for two things:

Total event_name hits

Total event_name hits where a particular criteria was met

This is my query so far. How can I improve line #3 so that the second count occurs as a nested WHERE function while the first count queries the full table? Thanks.

SELECT

COUNT (event_name) AS total_events,

COUNT (event_name) AS goal WHERE event_name = 'visited x page',

FROM \foodotcom-app-plus-web.analytics_1234567.events_20200809``
2

2 Answers

1
votes
select
  count(event_name) as total_events,
  count(case when event_name = 'visited x page' then event_name else null end) as goal
from `project.dataset.table`
1
votes

Below is for BigQuery Standard SQL

#standardSQL
SELECT
  COUNT(event_name) AS total_events,
  COUNTIF(event_name = 'visited x page') AS goal
FROM `project.dataset.table`