简体   繁体   中英

Is there a way to calculate the average number of times an event happens when all data is stored as string?

I am working in BigQuery and using SQL to calculate the average number of ads viewed per user based on their engagement level (levels range from 1 - 5). I previously calculated the average number of days users were active based on their engagement level, but when I do average number of ads viewed based on engagement level the query fails. My guess is that the string for ads viewed is stored as a string.

Is there a way to average the number of times 'ad viewed' occurs in a list of events, based on engagement?

I tried changing the original code I used where I extracted 'Average Days' to extract 'Ads Viewed' but that does not work.

I tried average(count(if(ads.viewed,1,0))), but that won't work either. I can't figure out what I am doing wrong.

I also checked this post ( SQL average of string values ) but this doesn't seem to apply.

    SELECT
      engagement_level,
      COUNT(event="ADSVIEWED") AS AverageAds

I have also tried:

    SELECT
      engagement_level,
      AVG(IF(event="ADSVIEWED",1,0)) AS AverageAds

But that doesn't work either.

It should put out a table of the engagement level with the corresponding average. For 'Average Days' it worked out to be Engagement Level: Average Days (1: 2.45, 2: 3.21, 3: 4.67, etc.). But it doesn't work for the ads_viewed event.

... to calculate the average number of ads viewed per user based on their engagement level...

Below is for BigQuery Standard SQL

#standardSQL
SELECT engagement_level, AVG(Ads) AverageAds FROM (
  SELECT engagement_level, user_id, COUNTIF(event = 'ADSVIEWED') Ads
  FROM `project.dataset.table`
  GROUP BY engagement_level, user_id
)
GROUP BY engagement_level

You can test, play with above using dummy data like in below example

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 user_id, 1 engagement_level, 'ADSVIEWED' event UNION ALL
  SELECT 1, 1, 'a' UNION ALL
  SELECT 1, 1, 'ADSVIEWED' UNION ALL
  SELECT 2, 1, 'b' UNION ALL
  SELECT 2, 1, 'ADSVIEWED'
)
SELECT engagement_level, AVG(Ads) AverageAds FROM (
  SELECT engagement_level, user_id, COUNTIF(event = 'ADSVIEWED') Ads
  FROM `project.dataset.table`
  GROUP BY engagement_level, user_id
)
GROUP BY engagement_level

with result

Row engagement_level    AverageAds   
1   1                   1.5  

If I understand correctly, you can do this without a subquery:

SELECT engagement_level,
       COUNTIF(event = 'ADSVIEWED') / COUNT(DISTINCT user_id) as avg_per_user
FROM t
GROUP BY engagement_level;

This counts the number of events and divides by the number of users. If you only want to count users who have the event:

SELECT engagement_level,
       COUNT(*) / COUNT(DISTINCT user_id) as avg_per_user
FROM t
WHERE event = 'ADSVIEWED'
GROUP BY engagement_level;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM