我在Google BigQuery上有一个带有位置的数据表,称为TABLE_A。
这是TABLE_A的样子:
df_nan <- data.frame(
Variable = colnames(df))
df_nan["%NA"] <- apply(df,2,function(x) round(sum(is.na(x))/NROW(x)*100,2)) #percentage over total
df_nan["#NA"] <- apply(df,2,function(x) sum(is.na(x))) #NA count
df_nan["%NA Y=1"] <- apply(df[df$Y == 1,],2,function(x) round(sum(is.na(x))/NROW(x)*100,2)) #percentage over total if Y=1
df_nan["%NA Y=0"] <- apply(df[df$Y == 0,],2,function(x) round(sum(is.na(x))/NROW(x)*100,2)) #percentage over total if Y=0
df_nan["#NA Y=1"] <- apply(df[df$Y == 1,],2,function(x) sum(is.na(x))) #NA count if Y=1
df_nan["#NA Y=0"] <- apply(df[df$Y == 0,],2,function(x) sum(is.na(x))) #NA count if Y=0
还有另一个包含不同项目的表,称为TABLE_B。 TABLE_B具有与TABLE_A相同的架构。这是来自TABLE_B的示例:
ID,Lat,Lon
1,32.95,65.567
2,33.95,65.566
,我想创建一个新表TABLE_C,其中每一行都包含TABLE_A和TABLE_B中的项,以使这些项最接近(即,联接表时经纬度对之间的距离是最小距离) 。这将是带有以上示例数据的TABLE_C的示例:
ID,Lat,Lon
a,32.96,65.566
b,33.96,65.566
我的实际数据是一方面带经纬度对,另一方面带ID_A,ID_B
1,a
2,b
的属性表(我正在寻找每个属性最近的气象站)。
答案 0 :(得分:1)
以下是用于BigQuery标准SQL
#standardSQL
SELECT AS VALUE ARRAY_AGG(STRUCT<id_a INT64, id_b STRING>(a.id, b.id) ORDER BY ST_DISTANCE(a.point, b.point) LIMIT 1)[OFFSET(0)]
FROM (SELECT id, ST_GEOGPOINT(lon, lat) point FROM `project.dataset.table_a`) a
CROSS JOIN (SELECT id, ST_GEOGPOINT(lon, lat) point FROM `project.dataset.table_b`) b
GROUP BY a.id
您可以使用问题中的伪数据作为
进行测试,玩耍#standardSQL
WITH `project.dataset.table_a` AS (
SELECT 1 id, 32.95 lat, 65.567 lon UNION ALL
SELECT 2, 33.95, 65.566
), `project.dataset.table_b` AS (
SELECT 'a' id, 32.96 lat, 65.566 lon UNION ALL
SELECT 'b', 33.96, 65.566
)
SELECT AS VALUE ARRAY_AGG(STRUCT<id_a INT64, id_b STRING>(a.id, b.id) ORDER BY ST_DISTANCE(a.point, b.point) LIMIT 1)[OFFSET(0)]
FROM (SELECT id, ST_GEOGPOINT(lon, lat) point FROM `project.dataset.table_a`) a
CROSS JOIN (SELECT id, ST_GEOGPOINT(lon, lat) point FROM `project.dataset.table_b`) b
GROUP BY a.id
有结果
Row id_a id_b
1 1 a
2 2 b