Google BigQuery 分析函数 - MAX(FUNCTION)
Google BigQuery Analytic Functions - MAX(FUNCTION)
我正在尝试编写一个查询,该查询将 return 与给定 关键字 关联的 最大搜索量 其对应的url.
示例数据集如下所示:
我目前的语法是:
理想情况下,查询将 return 搜索量最高的关键字及其 url 对。
我不确定我是否需要使用分析函数,或者我是否可以只创建一个自连接?各种方法都试过了,但运气不好。
如有任何帮助,我们将不胜感激。
Ideally, the query would return the keyword with the highest search volume along with its url pair.
它看起来就像你想要order by
和limit
:
select t.*
from mytable t
order by search_vol desc limit 1
这为您提供了整个 table 中 search_vol
最大的行。
另一方面,如果您想要每个 url 中搜索次数最多的关键字 ,那么它就是每组最大 n 个问题。这是本着您最初尝试的精神的解决方案,使用分析函数:
select t.* except (rn)
from (
select t.*, rank() over(partition by url order by search_vol desc) rn
from mytable t
) t
where rn = 1
rank()
允许顶部连接(如果有)。如果你想避免这种情况,请使用 row_number()
.
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT AS VALUE ARRAY_AGG(t ORDER BY search_vol DESC LIMIT 1)[OFFSET(0)]
FROM `project.dataset.table` t
GROUP BY url
我正在尝试编写一个查询,该查询将 return 与给定 关键字 关联的 最大搜索量 其对应的url.
示例数据集如下所示:
我目前的语法是:
理想情况下,查询将 return 搜索量最高的关键字及其 url 对。
我不确定我是否需要使用分析函数,或者我是否可以只创建一个自连接?各种方法都试过了,但运气不好。
如有任何帮助,我们将不胜感激。
Ideally, the query would return the keyword with the highest search volume along with its url pair.
它看起来就像你想要order by
和limit
:
select t.*
from mytable t
order by search_vol desc limit 1
这为您提供了整个 table 中 search_vol
最大的行。
另一方面,如果您想要每个 url 中搜索次数最多的关键字 ,那么它就是每组最大 n 个问题。这是本着您最初尝试的精神的解决方案,使用分析函数:
select t.* except (rn)
from (
select t.*, rank() over(partition by url order by search_vol desc) rn
from mytable t
) t
where rn = 1
rank()
允许顶部连接(如果有)。如果你想避免这种情况,请使用 row_number()
.
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT AS VALUE ARRAY_AGG(t ORDER BY search_vol DESC LIMIT 1)[OFFSET(0)]
FROM `project.dataset.table` t
GROUP BY url