SQL:根据计数从组中选择顶部元素



我有三个表:

  1. 查询的第一块:它计算印象

  2. 第二块查询:它计算转换

  3. 然后我加入创建另一个表


create table stage_II_final_suzuki_1648436600 as
select a.*,b.conversions, b.total_users 
from tmp.stage_II_imps_suzuki_1648436600 a
join 
tmp.stage_II_conversions_suzuki_1648436600 b
on a.domain = b.domain
and a.ad_position = b.ad_position
and a.browser_id = b.browser_id
and a.hour_of_day = b.hour_of_day
and a.day_of_week = b.day_of_week
and a.exchange_id = b.exchange_id
and a.device_type = b.device_type
ORDER BY b.total_users;

输出为:

域,ad_position,hour_of_day,browser_id,exchange_id,day_of_week,device_type,印象,转换,总用户

现在,我该如何返回满足80%用户需求的功能值(约20%(。?

功能:域、ad_position、browser_id、hour_of_day、day_of_week、exchange_id、device_type

例如,

浏览器用户共享铬-55%Mozila FF-17%MS Edge-12%Safari-15%鸭鸭go-0.5%然后结果应该是Chrome、Mozila和Safari

类似地,对于所有特征

此查询在子查询中使用sum(share) over (order by share desc)来获取市场份额的运行总数
它包括总市场份额超过80%的最低浏览器数量
NB这是在dbFiddle SQL服务器上测试的,但我相信Google bigQuery(由Google广告数据中心使用(具有所使用的所有功能。

with shares as
( select browser, share,
sum(share) over (order by share desc) sum_shares
from browser_user_share) ,
over80 as
( select max(share) s80
from shares
where sum_shares >= 80 )
select browser, share, sum_shares
from shares,
over80
where share >= over80.s80;
browser|share|sum_shares:--------|----:|--------:铬|55.00|55.00Mozila FF | 17.00 | 72.00Safari | 15.00 | 87.00

db<gt;小提琴这里

最新更新