我对 postgres 很陌生,并且遇到了一些非常困难的事情,我非常需要。此外,我没有使用合适的编辑器,它是某种形式的基于网络的编辑器。请考虑一下。
这是我的查询:
select coalesce('user') as user_src,
coalesce(root_domain(hostname), hostname, 'unknown') as web_domain,
count (*) as nohits
from $log
where coalesce(root_domain(hostname), hostname, 'unknown') in
(select coalesce(root_domain(hostname), hostname, 'unknown') as web_domain
from $log
group by web_domain
limit 10
)
group by user_src, web_domain
order by user_src, web_domain, nohits desc
但结果看起来不像我希望它们看起来的样子。我想拥有所有用户 + 他们的前 10 个网站。现在我看到所有用户和总共10 个网站,分为所有用户。-> 一些用户只有 0 个,因为他们从未访问过前 10 个用户之一。
谢谢你调查它!
编辑:这就是我如何转换它(不工作 - 这个错误:错误:列“主机名”不存在)
select coalesce('user') as user_src,
coalesce(root_domain(hostname), hostname, 'unknown') as web_domain,
count (*) as nohits
from
(select coalesce('user') as user_src,
coalesce(root_domain(hostname), hostname, 'unknown') as web_domain,
count (*) as nohits,
rank() over (partition by coalesce('user') order by coalesce('user'), count (*) desc) as rank
from $log
group by user_src, web_domain) w
where rank <= 2
order by user_src, rank
这会起作用,例如:(只是为了确保“主机名”存在)
select coalesce('user') as user_src,
coalesce(root_domain(hostname), hostname, 'unknown') as web_domain,
count (*) as nohits
from $log
group by user_src, web_domain
order by user_src, nohits