![](/img/trans.png)
[英]Return all rows using Count statement and Left Join with a Where Clause
[英]How to join results of different with clause
我有以下数据结构:
table user
user_id | year_month | fruit
------------------------------
1 | 2021-01 | orange
1 | 2021-01 | apple
1 | 2021-01 | melon
1 | 2021-01 | orange
1 | 2021-02 | kiwi
1 | 2021-02 | orange
1 | 2021-02 | banana
1 | 2021-02 | melon
1 | 2021-02 | orange
1 | 2021-03 | melon
1 | 2021-03 | orange
1 | 2021-03 | melon
1 | 2021-03 | banana
1 | 2021-03 | apple
我想生成这个 output
user_id | year_month | fruits | most_frequent
---------------------------------------------------------------------
1 | 2021-01 | orange, apple, melon | orange
1 | 2021-02 | kiwi, orange, banana, melon | orange
1 | 2021-03 | melon, orange, banana, apple | orange
它基本上是year_month
消耗的一组水果。 请注意,最后一列是一直以来消耗最多的水果,而不仅仅是一个月。
到目前为止,这是我的代码:
WITH user AS (
-- logic to retrieve the results from 2 other tables and aggregate into 1 temporary table
-- contains a function inside
), ranking AS (
SELECT
user_id,
fruit,
COUNT(*) AS most_frequent,
FROM
user
GROUP BY
1, 2
ORDER BY
3 desc
-- LIMIT 1
)
SELECT
user.user_id,
user.year_month,
STRING_AGG(distinct user.fruit, ', ') as fruits,
FROM user
LEFT JOIN ranking
ON user.user_id = ranking.user_id
WHERE user.user_id = 1
GROUP BY 1
, 2
ORDER BY 2
这是我的 output:
user_id | year_month | fruits
-----------------------------------------------------
1 | 2021-01 | orange, apple, melon
1 | 2021-02 | kiwi, orange, banana, melon
1 | 2021-03 | melon, orange, banana, apple
我在使用最常见的列时遇到问题。 我最初的解决方案是取消注释WITH ranking AS
中的limit 1
行,并将ranking.fruit 包含到主ranking.fruit
中,但是这样我没有返回任何数据。
更让我困惑的是,如果我在with
子句之外运行此ranking
代码 - 包括使用limit
线,我会得到我在“初始解决方案”中提到的所需分组。 所以这个问题似乎与一些连接问题有关。
考虑以下方法
select * from (
select user_id, year_month,
string_agg(distinct fruit) as fruits
from user
group by user_id, year_month
) join (
select user_id, fruit
from user
group by user_id, fruit
qualify 1 = row_number() over(partition by user_id order by count(*) desc)
)
using (user_id)
如果应用于您问题中的示例数据 - output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.