将一些列作为STRUCT数组合并到现有数组

时间:2019-09-19 08:56:34

标签: google-bigquery

我有这个模式

enter image description here

我想获得一个单一的STRUCTS数组,它将(feature,numerical_value)与已经存在的categorical_value结构数组合并。请注意,类别也可以是空字符串,我们要跳过。

我设法做到了这一点,但我正在寻找另一种更短的方法:

select centroid_id,array_agg(struct(name,value) order by centroid_id) as cluster from (
select centroid_id,concat(feature,'_',category) as name,value 
    FROM
    ML.CENTROIDS(MODEL `modelv1`), unnest(categorical_value)
    where length(category)>0
union all
select centroid_id,feature as name,numerical_value as value
FROM
    ML.CENTROIDS(MODEL `modelv1`)
    where numerical_value is not null
) 
group by centroid_id
order by centroid_id

1 个答案:

答案 0 :(得分:1)

#standardSQL
SELECT centroid_id, 
  (
    SELECT ARRAY_AGG(STRUCT(name,value)) FROM (
      SELECT CONCAT(feature,'_',category) AS name,value FROM UNNEST(categorical_value)
      WHERE LENGTH(category)>0
      UNION ALL
      SELECT feature, numerical_value
    ) 
    WHERE value IS NOT NULL
  ) AS cluster
FROM
    ML.CENTROIDS(MODEL `modelv1`)   

OR

#standardSQL
SELECT centroid_id, 
  ARRAY(
    SELECT AS STRUCT name,value FROM (
      SELECT CONCAT(feature,'_',category) AS name,value FROM UNNEST(categorical_value)
      WHERE LENGTH(category)>0
      UNION ALL
      SELECT feature, numerical_value
    ) 
    WHERE value IS NOT NULL
  ) AS cluster
FROM
    ML.CENTROIDS(MODEL `modelv1`)