#standardSQL

# See what trio of events are leading up to a desired event within your app!
# Make sure you replace "spend_virtual_currency" down near the bottom there with the actual 
# event you're trying to target

SELECT
  s0,
  s1,
  s2,
  COUNT(*) AS count
FROM (
  SELECT
    user_pseudo_id,
    event_timestamp,
    event_name AS s0,
    LEAD (event_name, 1) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) AS s1,
    LEAD (event_name, 2) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) AS s2
  FROM
    `<dataset>.events_<date>`
  ORDER BY
    user_pseudo_id,
    event_timestamp )
WHERE
  s2 = "spend_virtual_currency"
GROUP BY
  s0,
  s1,
  s2
ORDER BY
  count DESC