简体   繁体   English

如何连接不同with子句的结果

[英]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.

相关问题 使用 Count 语句和 Left Join 与 Where 子句返回所有行 - Return all rows using Count statement and Left Join with a Where Clause BigQuery Standard SQL - 加入不同格式的日期 - BigQuery Standard SQL - Join on date with different formats 为什么这两种方法显示不同的结果? - Why do these two methods shows different results? 为什么 LEFT JOIN 左侧表的日期范围不限制以与 where 子句相同的方式处理的字节数? - Why does the date-range of the table on the left side of a LEFT JOIN not limit the bytes processed in the same way as a where clause? 在不同分组中获得汇总结果的更有效方法? - SQL - More efficient way to get aggregate results in different groupings? - SQL AWS.CloudWatch SDK 与 CLI 产生不同的结果 - AWS.CloudWatch SDK vs. CLI produces different results 如何处理更新查询的 where 子句中的多行 (BigQuery) - How to handle multiple lines in a where clause for an update query (BigQuery) 如何在 Cloud Firestore 查询中加入多个文档? - How to join multiple documents in a Cloud Firestore query? 如何将 AVG 计算为 2 个表之间的左连接? - How to calculate AVG into left join between 2 tables? 如何使用带有 FieldPat.documentId 的 where 子句过滤 firebase 数据 - How to filter firebase data using a where clause with FieldPat.documentId
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM