BigQuery两个表(是否加入)名称查询

时间:2019-02-11 22:07:46

标签: google-bigquery

我目前在Bigquery上有两个表,试图在其中查找特定地址的“英文名称”。下面的示例表。

表1

Address|Value
------------------
1111,55
2222,99
8888|33

表2

Address|Name
------------------
1111|Bob
2222|Jill
8888|Steven

查询后所需的结果

Address|Name|Value
------------------
1111|Bob|55
2222|Jill|99
8888|Steven|33

在BigQuery中完成此操作的任何帮助将不胜感激。

真实查询

SELECT CASE WHEN tablename.from_number = '1231231234' THEN tablename.to_number ELSE tablename.from_number END AS party,
       COUNT(*) AS call_count,
       tablename.caller_address AS caller_address
FROM `bigquery-database` tablename
WHERE '1231231234' in (tablename.from_number, tablename.to_number)
GROUP BY party, caller_address
ORDER by call_count DESC

总而言之,我试图查找存在于tablename2.firstname下另一个表中的caller_address的名字,并将其显示在查询输出中。我不知道如何使用(加入?)。

伪代码

SELECT CASE WHEN tablename.from_number = '1231231234' THEN tablename.to_number ELSE tablename.from_number END AS party,
       COUNT(*) AS call_count,
       tablename.caller_address AS caller_address,

       Find where tablename.caller_address equals db.tablename2.caller_address and display first name from db.tablename2.name

FROM `bigquery-database` tablename
WHERE '1231231234' in (tablename.from_number, tablename.to_number)
GROUP BY party, caller_address
ORDER by call_count DESC

1 个答案:

答案 0 :(得分:2)

我认为您需要一个简单的内部联接。

select t1.address, t2.name, t1.value
from table1 as t1
inner join table2 as t2 on t1.address=t2.address

BigQuery Documentation