简体   繁体   中英

How to count elements based on a unique value in BigQuery

I have this table 1 in Bigquery and I need to count the elements in column segments and category that correspond to a single user id. Desired outcome presented in table 2. I haven't been able to figure out how to do it... maybe transforming those elements to arrays?

TABLE 1

表格1

TABLE 2

表 2

Use below

select `desc`, count(distinct user_id) distinct_user_id
from (
  select category as `desc`, user_id from your_table 
  union all
  select segment, user_id from your_table, 
  unnest(split(segment, ';')) segment
)
where `desc` != ''
group by `desc`            

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