【发布时间】:2020-11-03 14:12:16
【问题描述】:
场景: 有两个 BigQuery 表 A 和 B,有多个列和公共键列。需要使用公共键列连接两个表,并从另一个表中获取各自的值,如下面的示例所述。
输入: 有两张桌子
表 A:
store category city
11 aaa xx
12 bbb yy
12 ccc zz
13 ddd xy
表 B:
store sale1 sale2 sale3
11 0.5 0.75 0.25
12 1.2 1.25 1.23
13 0.9 0.87 0.54
预期输出 - 结果表 C:
store category city sale
11 aaa xx 0.5
12 bbb yy 1.25
12 ccc zz 1.23
13 ddd xy 0.87
输出解释:
第 1 点: 使用公共列“store”连接两个表
第 2 点: 检查列 category == 'aaa',然后从表 B 中获取列 'sale1',如果类别在 ('bbb','ddd'),则获取列 ' sale2' 并且如果 category == 'ccc',则获取列 'sale3' 并将相应的值存储在结果表 C 中作为列 'sale'。
尝试过 BIGQUERY:
with res as
(select
a.store,
a.category,
a.city
)
SELECT store, category, city,
case
when category in ('aaa') then sale=b.sale1
when category in ('bbb','ddd') then sale=b.sale2
when category in ('ccc') then sale=b.sale3
end
as sale
FROM `tableA` AS a
JOIN `tableB` AS b
ON a.store = CAST(b.store AS STRING)
需要帮助。提前致谢!
【问题讨论】:
标签: sql join google-bigquery case