簡體   English   中英

Bigquery:比較兩個表中的列,看它們是否匹配

[英]Bigquery: Compare the columns in two tables to see if they match

我有兩個表(t1 和 t2),它們都包含 100 個名稱完全相同的列,如下所示:

表格1

ID 一種
123 1個 0
234 0 1個
345 1個 1個

表 2

ID 一種
123 1個 0
234 0 1個
345 1個 0

它們都有一個 id 列和僅包含 1 和 0 值的列。 我需要查看這些列是否相互匹配。 有什么方法可以構建如下表,顯示具有相同名稱的每一列的匹配百分比:

t1 t2 匹配?
一種 一種 100%
66%

提前致謝!

考慮以下方法

create temp function get_keys(input string) returns array<string> language js as """
  return Object.keys(JSON.parse(input));
""";
create temp function  get_values(input string) returns array<string> language js as """
  return Object.values(JSON.parse(input));
""";
with temp1 as (
  select id, key, value
  from table1 t, 
  unnest(get_keys(to_json_string(t))) key with offset 
  join unnest(get_values(to_json_string(t))) value with offset 
  using(offset)
  where key != 'id'
), temp2 as (
  select id, key, value
  from table2 t, 
  unnest(get_keys(to_json_string(t))) key with offset 
  join unnest(get_values(to_json_string(t))) value with offset 
  using(offset)
  where key != 'id'
)
select key, round(100 * countif(t1.value = t2.value) / count(t1), 2) matched
from temp1 t1
left join temp2 t2
using(key, id)
group by key    

如果應用於您問題中的示例數據 - output 是

在此處輸入圖像描述

暫無
暫無

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

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