- GA4 BigQuery Newsletter For Marketing Analytics
- Posts
- Combining Multiple Conditions to Narrow Down The GA4 Data in BigQuery SQL
Combining Multiple Conditions to Narrow Down The GA4 Data in BigQuery SQL
BigQuery With GA4
Combining Multiple Conditions to Narrow Down The GA4 Data in BigQuery SQL
In the previous lesson, we looked at the WHERE clause with one condition.
However, most analyses are not that simple. In most practical cases you will need to narrow down your result using multiple conditions.
Let’s take a look at how to do that.
Here is the SQL that we used in the previous lesson
SELECT * FROM `optizent-test-project.analytics_219941990.events_20221204`
WHERE event_name = 'first_visit'
Now, continuing with this example, let’s say we only want to look at the rows that have the “first_visit” event and are from the United States. In that case, you will use the “WHERE” clause in your statement to filter the result set based on two conditions
Event Name is first_visit
The country where that event took place is the United States
In this case, you will use the AND operator to combine two conditions:
To know which columns to use for this condition you will need to know how structure of the GA4 data in BigQuery (check out, GA4 Event Schema in BigQuery).
So the modified SQL will be
SELECT * FROM `optizent-test-project.analytics_219941990.events_20221204`
WHERE event_name = 'first_visit'
Here is the screenshot of the BigQuery console.
Now let’s change the scenario and say you want to look at those records where either the event name is first_visit or the country is the United States.
In this case, you will use the OR operator to combine these conditions
So the modified SQL will be
SELECT * FROM `Optizent-test-project.analytics_219941990.events_20221204`
WHERE event_name = 'first_visit'
Here is the screenshot of the BigQuery console. As you can see now we have a lot more events than just first_visit as the result set includes all events by sessions from the United States.
Advanced Section:
Reply