Skip to content

Instantly share code, notes, and snippets.

@lfy79001
Last active April 10, 2024 03:08
Show Gist options
  • Save lfy79001/516fd65df64165700ff00fcfb0ae7d51 to your computer and use it in GitHub Desktop.
Save lfy79001/516fd65df64165700ff00fcfb0ae7d51 to your computer and use it in GitHub Desktop.
WITH
Params AS (
SELECT 'Google Navy Speckled Tee' AS selected_product
),
PurchaseEvents AS (
SELECT
user_pseudo_id,
items
FROM
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
_TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'purchase'
),
ProductABuyers AS (
SELECT DISTINCT
user_pseudo_id
FROM
Params,
PurchaseEvents,
UNNEST(items) AS items
WHERE
items.item_name = selected_product
)
SELECT
items.item_name AS item_name,
SUM(items.quantity) AS item_quantity
FROM
Params,
PurchaseEvents,
UNNEST(items) AS items
WHERE
user_pseudo_id IN (SELECT user_pseudo_id FROM ProductABuyers)
AND items.item_name != selected_product
GROUP BY 1
ORDER BY item_quantity DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment