我有一个表hostapp_apps和一个带有列的视图host_app_usercount,如下所示
hostapp_apps
======================================================
host_app_id || app_type || app_id || platform || user_count
host_app_usercount
======================================================
host_id || TotalUsers
基本上我试图通过使用以下查询来获取app_id,platform,max(usercount),TotalUsers
SELECT hostapp_apps.app_type as app_type, host_app_usercount.TotalUsers as Total,
hostapp_apps.app_id as app_id, MAX(hostapp_apps.user_count) AS sum,
ROUND((MAX(hostapp_apps.user_count)/(host_app_usercount.TotalUsers) * 100 ),2) AS percentage
FROM pg_datascience.hostapp_apps LEFT OUTER JOIN
host_app_usercount
ON (host_app_usercount.host_id=hostapp_apps.host_app_id)
GROUP BY hostapp_apps.app_id, host_app_usercount.TotalUsers
HAVING sum >= 10000
ORDER BY percentage desc
limit 0, 50;
但获取数据需要很长时间。有没有办法在没有左外连接的情况下执行此操作。另外,我尝试了另一种方法,不使用连接
SELECT hostapp_apps.app_type as app_type,
(select TotalUsers
from host_app_usercount
where host_app_usercount.host_id=hostapp_apps.host_app_id
) as Total,
hostapp_apps.app_id as app_id, MAX(hostapp_apps.user_count) AS sum,
ROUND((MAX(hostapp_apps.user_count)/(Total) * 100 ),2) AS percentage
FROM pg_datascience.hostapp_apps
GROUP BY hostapp_apps.app_id, Total
HAVING sum >= 10000
ORDER BY percentage desc
limit 0, 50;
但是当我尝试这个时,我在字段列表中得到了未知的列'Total'。任何帮助都非常感谢,因为它影响了我的任务进度:(提前致谢
答案 0 :(得分:0)
首先,您的第一个查询无效。您的列app_type
不是聚合,不在Group By
子句中。该查询将给您一个错误。
其次,它可能是一个索引问题。您需要查看索引并可能创建它们。我会在以下各项中创建一个:
host_app_usercount.host_id
hostapp_apps.host_app_id
这应该有所帮助。