Bigquery:将具有空结构记录的行压缩为单个记录

问题描述 投票:0回答:1

使用 Bigquery。 我正在尝试合并 Array> 中的两列。 以下是我提出的查询。 如果两列中任一列的值为空或 null,我不想创建两个 null 结构记录,而是将其压缩为单个 null 记录。 绿色/蓝色框中的记录应该只是一条记录而不是两条空记录。

    SELECT  [
   case when (colA is not NULL and trim(colA) != "") 
    then struct("center" as key, colA as value) 
    else  struct(NULL as key, NULL as value) 
    end, 
   
   case when (colB is not NULL and trim(colB) != "") 
    then struct("owner" as key, colB as value) 
    else  struct(NULL as key, NULL as value) 
    end
  
  ] as labels
  FROM `project.dataset.bill`;

sql arrays google-cloud-platform struct google-bigquery
1个回答
0
投票

这是您的查询的修订版本。试试这个:

SELECT 
  CASE
    WHEN TRIM(IFNULL(colA, '')) = '' AND TRIM(IFNULL(colB, '')) = '' THEN [STRUCT(NULL AS key, NULL AS value)]
    ELSE ARRAY_CONCAT(
      IF(TRIM(IFNULL(colA, '')) != '', [STRUCT("center" AS key, colA AS value)], []),
      IF(TRIM(IFNULL(colB, '')) != '', [STRUCT("owner" AS key, colB AS value)], [])
    )
  END AS labels
FROM `project.dataset.bill`;
© www.soinside.com 2019 - 2024. All rights reserved.