Presto SQL 查询以计算观看百分比
Presto SQL query to calcute percent views
我必须计算每个类别的贡献百分比。
SELECT portfolio, (total_portfolio_views*1.00000/total_views)*100 view_contribution_perc
FROM(
select portfolio,sum(portfolio_views) AS total_portfolio_views, (select sum(portfolio_views) from gold.user_daily_osv) as total_views
from gold.user_daily_osv
group by 1
)
ORDER BY 2 DESC
虽然这工作正常,但执行起来需要很多时间,因为这些表很大,任何其他解决方法都会非常有用。
您可以尝试使用 sum
window function 来计算总观看次数:
SELECT portfolio,
(total_portfolio_views * 1.0 / sum(total_portfolio_views) over ()) * 100 view_contribution_perc
FROM(
select portfolio,
sum(portfolio_views) AS total_portfolio_views
from gold.user_daily_osv
group by 1
)
ORDER BY 2 DESC