解决复杂的 SQL-Based 聚合问题与多个事件
Solving Complex SQL-Based Aggregation problem with multiple events
长话短说,我刚才没有通过以下测试评估。想终于明白怎么解决了。
player_id:整数
game_id:整数
event_type: 可变字符(64)
event_time : 时间戳
store_game_install: 指定玩家已完成指定游戏的初始安装
store_game_update: 指定玩家对指定游戏进行了更新
store_open:指定玩家已在设备上启动游戏商店客户端
store_deleted: 指定玩家已卸载设备上的游戏商城客户端
store_game_download:指定玩家已将游戏包下载到
由于必须从 store_game_view 后跟 store_game_download.
推断出实际的游戏安装
另一方面,更高版本的商店客户端已停止生成 store_game_download 事件。
我必须构造一个 MySql/PostgreSQL 语句来生成以下聚合:
game_id => game id
game_views => Total count of game detail views for this
game_installs => Total count of game installs for this game
任何帮助都意味着整个世界。谢谢!
您可以进行条件聚合。
在 Postgres 中:
select
app_id,
count(*) filter(where event_type = 'store_app_view') app_views,
count(*) filter(where event_type = 'store_app_install') app_installs
count(*) filter(where event_type = 'store_app_install')
/ count(*) filter(where event_type = 'store_app_view') app_conversion_rate
from events_db
group by app_id
实施逻辑来推断应用程序旧版本的商店安装有点复杂。为此,一种方法是使用 window 函数:
select
app_id,
count(*) filter(where event_type = 'store_app_view') app_views,
count(*) filter(where event_type = 'store_app_install' or event_type = 'store_app_view' and lead_event_type = 'store_app_download') app_installs
count(*) filter(where event_type = 'store_app_install' or event_type = 'store_app_view' and lead_event_type = 'store_app_download')
/ count(*) filter(where event_type = 'store_app_View') app_conversion_rate
from (
select
e.*,
lead(event_type) over(partition by user_id order by event_type) lead_event_type
from events_db e
) t
group by app_id
在 MySQL 中,它将与为 Postgres 显示的@GMB 接近相同
SELECT
`app_id`,
SUM(`event_type` = 'store_app_view') app_views,
SUM(`event_type` = 'store_app_install') app_installs,
(SUM(`event_type` = 'store_app_install') / SUM(`event_type` = 'store_app_view')) app_conversion_rate
FROM `events_db`
GROUP BY `app_id`
长话短说,我刚才没有通过以下测试评估。想终于明白怎么解决了。
player_id:整数 game_id:整数 event_type: 可变字符(64) event_time : 时间戳
store_game_install: 指定玩家已完成指定游戏的初始安装
store_game_update: 指定玩家对指定游戏进行了更新
store_open:指定玩家已在设备上启动游戏商店客户端 store_deleted: 指定玩家已卸载设备上的游戏商城客户端 store_game_download:指定玩家已将游戏包下载到 由于必须从 store_game_view 后跟 store_game_download.
推断出实际的游戏安装另一方面,更高版本的商店客户端已停止生成 store_game_download 事件。
我必须构造一个 MySql/PostgreSQL 语句来生成以下聚合:
game_id => game id
game_views => Total count of game detail views for this
game_installs => Total count of game installs for this game
任何帮助都意味着整个世界。谢谢!
您可以进行条件聚合。
在 Postgres 中:
select
app_id,
count(*) filter(where event_type = 'store_app_view') app_views,
count(*) filter(where event_type = 'store_app_install') app_installs
count(*) filter(where event_type = 'store_app_install')
/ count(*) filter(where event_type = 'store_app_view') app_conversion_rate
from events_db
group by app_id
实施逻辑来推断应用程序旧版本的商店安装有点复杂。为此,一种方法是使用 window 函数:
select
app_id,
count(*) filter(where event_type = 'store_app_view') app_views,
count(*) filter(where event_type = 'store_app_install' or event_type = 'store_app_view' and lead_event_type = 'store_app_download') app_installs
count(*) filter(where event_type = 'store_app_install' or event_type = 'store_app_view' and lead_event_type = 'store_app_download')
/ count(*) filter(where event_type = 'store_app_View') app_conversion_rate
from (
select
e.*,
lead(event_type) over(partition by user_id order by event_type) lead_event_type
from events_db e
) t
group by app_id
在 MySQL 中,它将与为 Postgres 显示的@GMB 接近相同
SELECT
`app_id`,
SUM(`event_type` = 'store_app_view') app_views,
SUM(`event_type` = 'store_app_install') app_installs,
(SUM(`event_type` = 'store_app_install') / SUM(`event_type` = 'store_app_view')) app_conversion_rate
FROM `events_db`
GROUP BY `app_id`