[英]How to join results of different with clause
I have the following data structure:我有以下数据结构:
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
I want to generate this output我想生成这个 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
It is basically a grouping of the fruits consumed by year_month
.它基本上是
year_month
消耗的一组水果。 Notice that the last column it is the most consumed fruit of all the time and not only the month.请注意,最后一列是一直以来消耗最多的水果,而不仅仅是一个月。
This is my code so far:到目前为止,这是我的代码:
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
and this is my output:这是我的 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
I'm having problems with the most frequent column.我在使用最常见的列时遇到问题。 My initial solution was to uncomment that
limit 1
line in the WITH ranking AS
and include ranking.fruit
into the main select, but this way I got no data returned.我最初的解决方案是取消注释
WITH ranking AS
中的limit 1
行,并将ranking.fruit 包含到主ranking.fruit
中,但是这样我没有返回任何数据。
To be more confusing for me, if I run this ranking
code outside the with
clause - including with the limit
line, I got the desired grouping I mentioned in the 'initial solution'.更让我困惑的是,如果我在
with
子句之外运行此ranking
代码 - 包括使用limit
线,我会得到我在“初始解决方案”中提到的所需分组。 So the problem seems to be related to some join question.所以这个问题似乎与一些连接问题有关。
Consider below approach考虑以下方法
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)
if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.