【问题标题】:Joining two tables via a third mapping table in BigQuery / SQL通过 BigQuery / SQL 中的第三个映射表连接两个表
【发布时间】:2022-01-15 19:21:03
【问题描述】:

我想知道你是否可以帮助我。我想从 table1 中选择一些列并将它们与表 2 中的一些列合并,使用将 customer_id 映射到 cust_num 的表 3。

表 1

customer_id account balance account_type
1 A 100 A
2 B 200 B
3 C 300 B

表 2

cust_num score1 score2 score3
1234 10 100 1000
2345 20 200 2000
3456 30 300 3000

表 3

cust_id ref
1234 1
2345 2
3456 3

在表 3 中,ref= 表 1 的 customer_id 和 cust_id = 表 2 的“cust_num”。我无法更改任何变量或列名。

理想情况下,我想从表 1 中选择账户和余额,并将它们与表 2 中的分数 1 和分数 2 匹配以结束

cust_num account balance score1 score2
1234 A 100 10 100
2345 B 200 20 200
34567 C 300 30 300

提前致谢!

【问题讨论】:

    标签: sql join


    【解决方案1】:

    两个内部连接应该可以完成这项工作:

    SELECT cust_num,
       account,
       balance,
       score1,
       score2
    FROM   ((table1
         INNER JOIN table3
                 ON table3.ref = table1.customer_id)
        INNER JOIN table2
                ON table3.cust_id = table2.cust_num) 
    

    【讨论】:

      【解决方案2】:

      使用连接

      SELECT t3.cust_id,t1.account,t1.balance,t3.score1,t3.score2
      FROM table1 t1
      JOIN table3 t2 ON t1.customer_id = t2.ref
      JOIN table2 t3 ON t2.cust_id = t3.cust_num
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多