【问题标题】:Need top 5 searched keywords from sum of searched words需要搜索词总和中搜索到的前 5 个关键词
【发布时间】:2022-02-18 06:06:24
【问题描述】:
| Category |
Searched Word |
Search Count |
Date |
| Clothing |
Men Shirt |
3544 |
2022-01-19 |
| Footwear |
Shoes |
7293 |
2022-02-02 |
| Mobile |
Iphone |
4901 |
2022-02-12 |
| Clothing |
Hoodie |
2049 |
2021-12-30 |
| Footwear |
crocs |
3328 |
2022-01-28 |
| Clothing |
Men shirt |
903 |
2022-02-12 |
我正在研究 bigquery,从上表中,我需要根据日期 2022-01-01 到 2022-02-14 之间的搜索计数列的总和,找到每个类别的前 5 个搜索词,我已经尝试过 SQL windows 功能,但无法解决。任何帮助表示赞赏。
【问题讨论】:
标签:
sql
group-by
google-bigquery
sum
【解决方案1】:
考虑以下任何一种方法
选项 1
select Category, string_agg(SearchedWord, ', ' order by totalCount desc limit 5) topFiveSearchedWord
from (
select Category, initcap(SearchedWord) SearchedWord, sum(SearchCount) totalCount
from your_table
where date between '2022-01-01' and '2022-02-14'
group by Category, SearchedWord
)
group by Category
选项 2
select Category,
( select string_agg(value, ', ' order by sum desc)
from t.arr
) topFiveSearchedWord
from (
select Category, approx_top_sum(initcap(SearchedWord), SearchCount, 5) Arr
from your_table
where date between '2022-01-01' and '2022-02-14'
group by Category
) t