How do I take out data from an event for multiple parameters with value of one parameter being the same in the event

Deadly 提交于 2020-01-06 05:27:04

问题


Take for example,

event_dim.name = "Start_Level"
event_dim.params.key = "Chapter_Name"
event_dim.params.value.string_value = "chapter_1" (or "chapter_2" or "chapter_3" and so on)
event_dim.params.key = "Level"
event_dim.params.value.int_value = 1 or 2 or 3 or 4 and so on
event_dim.params.key = "Opening_Balance"
event_dim.params.value = 1000 or 1200 or 300 or so on

How do I take out the data if I want to: - Look at unique users who've played "Level" only for event_dim.params.string_value = "chapter_1" (meaning for levels in Chapter 1) - Look at the "Opening_Balance" per "Level" only the levels in the chapter where event_dim.params.key = "Chapter_Name" and event_dim.params.value.string_value = "chapter_2"

Currently, I am trying to do it as below to grab the data which I don't think is giving me proper data. I am trying to take out level data for users who've installed the game between a particular date (through first_open) and from a particular source.:

SELECT
  COUNT(DISTINCT(app_instance)),
  event_value.int_value
FROM (
  SELECT
    user_dim.app_info.app_instance_id AS app_instance, 
    event.name AS event,
    (
    SELECT
      user_prop.value.value.int_value
    FROM
      UNNEST(user_dim.user_properties) AS user_prop
    WHERE
      user_prop.key = 'first_open_time') AS first_open,
    params.key AS event_param,
    params.value AS event_value
  FROM
    `app_package.app_events_*`,
    UNNEST(event_dim) AS event,
    UNNEST(event.params) AS params
  WHERE
    event.name = "start_level"
    AND user_dim.traffic_source.user_acquired_source = "source"
    AND params.key != 'firebase_event_origin'
    AND params.key != 'firebase_screen_class'
    AND params.key != 'firebase_screen_id' )
WHERE
  event_param = "Level"
  AND (first_open >= 1516579200000 AND first_open <= 1516924800000)
GROUP BY
  event_value.int_value

However, I am not able to segregate events which are specific to when chapter_name = "chapter_1" in the event. (I don't know how to do it unfortunately and hence the question)

Update: (Some additional information added as requested by Mikhail)

Sample Input events would be as follows:

+-----------------+-------------+-----------------+--------------+-----------+
| app_instance_id | event_name  |    param_key    | string_value | int_value |
+-----------------+-------------+-----------------+--------------+-----------+
|          100001 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 1         |
|                 |             | opening_balance | null         | 2000      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 2500      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 2750      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 3         |
|                 |             | opening_balance | null         | 3000      |
|                 | start_level | chapter_name    | chapter_2    | null      |
|                 |             | level           | null         | 1         |
|                 |             | opening_balance | null         | 3100      |
|                 | start_level | chapter_name    | chapter_2    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 3500      |
|                 | start_level | chapter_name    | chapter_2    | null      |
|                 |             | level           | null         | 3         |
|                 |             | opening_balance | null         | 3800      |
|          100002 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 1         |
|                 |             | opening_balance | null         | 2000      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 2250      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 2400      |
|                 | start_level | chapter_name    | chapter_1    | null      |
|                 |             | level           | null         | 3         |
|                 |             | opening_balance | null         | 2800      |
|                 | start_level | chapter_name    | chapter_2    | null      |
|                 |             | level           | null         | 1         |
|                 |             | opening_balance | null         | 3000      |
|                 | start_level | chapter_name    | chapter_2    | null      |
|                 |             | level           | null         | 2         |
|                 |             | opening_balance | null         | 3200      |
+-----------------+-------------+-----------------+--------------+-----------+

Output required is as follows:

+-----------+-------+--------------+-------------------+---------------+
|  Chapter  | Level | Unique Users | Total Level Start | Avg. Open Bal |
+-----------+-------+--------------+-------------------+---------------+
| chapter_1 |     1 |            2 |                 2 |          2000 |
| chapter_1 |     2 |            2 |                 3 |          2383 |
| chapter_1 |     3 |            2 |                 3 |          2850 |
| chapter_2 |     1 |            2 |                 2 |          3050 |
| chapter_2 |     2 |            2 |                 2 |          3350 |
| chapter_2 |     3 |            1 |                 1 |          3800 |
+-----------+-------+--------------+-------------------+---------------+

回答1:


For anyone who is looking for an answer to this question, you can try the below standard sql query:

SELECT
    chapter,
    level,
    count(distinct id) as Unique_Users,
    count(id) as Level_start,
    avg(opening_balance) as Avg_Open_Bal,
FROM(
SELECT
    user_dim.app_info.app_instance_id AS id,
    event.date,
    event.name,
    (SELECT value.string_value FROM UNNEST(event.params) WHERE key = "chapter_name") AS chapter,
    (SELECT value.int_value FROM UNNEST(event.params) WHERE key = "level") AS level,
    (SELECT value.int_value FROM UNNEST(event.params) WHERE key = "opening_coin_balance") AS open_bal
  FROM
    `<table_name>`,
    UNNEST(event_dim) AS event
  WHERE
    event.name = "start_level"
)
GROUP BY
    chapter,
    level


来源:https://stackoverflow.com/questions/48544392/how-do-i-take-out-data-from-an-event-for-multiple-parameters-with-value-of-one-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!