sql - Calculating a current day 7 day active user with BigQuery? -


to calculate current day active user should simple if i'm not mistaken. take today , x days (7-day-active 6 back) , count distinct ids. have following query 2 day active user:

with alltables (   select      concat(user_dim.app_info.app_id, ':', user_dim.app_info.app_platform) app,     event.date,     user_dim.app_info.app_instance_id users   `dataset.app_events_intraday_20170407`   cross join     unnest(event_dim) event    union   select      concat(user_dim.app_info.app_id, ':', user_dim.app_info.app_platform) app,     event.date,     user_dim.app_info.app_instance_id users   `dataset.app_events_20170406`   cross join     unnest(event_dim) event ) select count(distinct(users)) unique,    count(users) total alltables 

this 2-day active 7day or 30day union tables on. correct or need modification?

instead of using union should try use querying multiple tables using wildcard table

try below

#standardsql alltables (   select      concat(user_dim.app_info.app_instance_id, ':', user_dim.app_info.app_platform) app,     event.date,     user_dim.app_info.app_instance_id users   `dataset.app_events_intraday_*`, unnest(event_dim) event   _table_suffix between '20170401' , '20170407'    union   select      concat(user_dim.app_info.app_instance_id, ':', user_dim.app_info.app_platform) app,     event.date,     user_dim.app_info.app_instance_id users   `dataset.app_events_*`, unnest(event_dim) event   _table_suffix between '20170401' , '20170407'  )  select    count(distinct(users)) unique,   count(users) total alltables 

you can use below where clause make more generic

where _table_suffix     between format_date('%y%m%d', date_sub(current_date(), interval 6 day))     , format_date('%y%m%d', current_date()) 

also please note: changed app_id in user_dim.app_info.app_id app_instance_id thought typo on side - can wrong


Comments

Popular posts from this blog

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -