postgresql 具有计数聚合的SQL查询

tpgth1q7  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(107)

如何返回加州州的城市及其安装率(安装应用的用户数占有活动的用户数),并按从高到低的顺序排列?Table name = Events

SELECT location, COUNT(*) AS num_of_installs
FROM data_mapping.Events
WHERE location LIKE '%California' AND event_type='install'
GROUP BY location
oyxsuwqo

oyxsuwqo1#

我不想回答你的问题,但你可以使用window functions来计算利率。请检查下一个查询:

SELECT DISTINCT 
    location, 
    COUNT(*) OVER (PARTITION BY location) AS location_installs,
    COUNT(*) OVER () AS num_of_installs,
    (COUNT(*) OVER (PARTITION BY location))::numeric / COUNT(*) OVER () AS ratio
FROM Events
WHERE location LIKE '%California' AND event_type='install';

SQLize - online SQL editor

相关问题