简体   繁体   English

BigQuery 联接表

[英]BigQuery Join Tables

Example, I have 2 tables.例如,我有 2 个表。

Pet宠物

PetID宠物ID PetName宠物名称 PetType宠物类型
123 123 Charlie查理 Dog
456 456 Rose玫瑰 Cat
789 789 Sam山姆 Rabbit兔子

Owner所有者

PetID宠物ID OwnerName所有者姓名 OwnerNumber所有者编号
123 123 Phil菲尔 1234567 1234567
123 123 Carly卡莉 2345678 2345678
456 456 Russel罗素 3456789 3456789
456 456 Sophie苏菲 4567891 4567891
789 789 Tasha塔莎 5678912 5678912
890 890 Bill账单 6789012 6789012
901 901 Shay谢伊 7890123 7890123

I want the result to be:我希望结果是:

PetID宠物ID OwnerName所有者姓名 OwnerNumber所有者编号 PetName宠物名称
123 123 Phil菲尔 1234567 1234567 Charlie查理
456 456 Russel罗素 3456789 3456789 Rose玫瑰
789 789 Tasha塔莎 5678912 5678912 Sam山姆

I only want the PetId's that are in Pet.我只想要 Pet 中的 PetId。 I only want distinct PetId's.我只想要不同的 PetId。 How would I go about this in BigQuery?我将如何在 BigQuery 中解决这个问题? Inner Join?内部联接?

Thank you谢谢

I would rather go with我宁愿和

select o.*, PetName
from owner o
join pet p
using (PetID)
where true 
qualify row_number() over(partition by PetID) = 1    

If applied to sample data in your question - output is如果应用于您问题中的样本数据 - 输出为

在此处输入图片说明

This looks like a simple join but you have to restrict to a single pet id.这看起来像一个简单的join但您必须限制为单个宠物 ID。 You can use qualify :您可以使用qualify

select p.*, o.ownernumber
from pet p join
     owner o
     using (petid)
where 1=1
qualify row_number() over (partition by petid order by petid) = 1;

You can use the order by to control which owner you get.您可以使用order by来控制您获得的所有者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM