[英]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.