簡體   English   中英

如何比較 Bigquery 中的多個表並將結果放在不同的列中?

[英]How can I compare multiple tables in Bigquery and get the results in in separate columns?

我在同一個 BigQuery 數據集中有大約 3 個不同的表,它們都具有相同的架構。 我需要比較所有表中是否存在多個值,並將結果列在單獨的列中。

例如:表格:“豪華”、“經濟”和“SUV”。 它們都有“顏色”、“價格”和“尺寸”列。

我想查詢所有表以獲取顏色 = 'red' 或 color = 'blue' 的表。

這應該只返回兩行 - 比如:

奢華 經濟 越野車
紅色的 紅色的 紅色的
藍色的

所以在這種情況下,Economy 表有紅色和藍色汽車,而 Luxury 和 SUV 則只有紅色汽車。 我試過 UNION ALL 和 INNER JOIN,但它們只返回一行

謝謝

考慮以下方法

select * except(pos) from (
  select * from (
    select distinct color, 'Luxury' source, row_number() over(order by color) pos from Luxury union all
    select distinct color, 'Economy' source, row_number() over(order by color) pos from Economy union all
    select distinct color, 'SUVs' source, row_number() over(order by color) pos from SUVs 
  )
  where color in ('red', 'blue')
)
pivot (any_value(color) for source in ('Luxury', 'Economy', 'SUVs'))

與 output 如下

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM