简体   繁体   中英

How to get a query array inside a query array in BigQuery?

I have a table that stores Stock as JSON data type.

I need to write a query that returns the ProductID and Quantity in this output format:

在此处输入图像描述

However the most close to it I managed to do is this output:

SELECT

  GenerationTime
  JSON_QUERY_ARRAY(Stock.Sellable) Stock_Sellable_ProductID
  ,OrganizationUnit.IsDeleted OrganizationUnit_IsDeleted
  ,OrganizationUnit.Description OrganizationUnit_Description
  ,OrganizationUnit.BranchNumber
  ,OrganizationUnit.BranchNumber
  
FROM
  `....xt_...stock`
  
where GenerationTime = timestamp("2022-08-09 01:00:28.169503 UTC")

在此处输入图像描述

I also managed to use UNNEST but this will repeat the GenerationTime four times:

SELECT

  GenerationTime
  ,s.ProductID
  ,OrganizationUnit.IsDeleted OrganizationUnit_IsDeleted
  ,OrganizationUnit.Description OrganizationUnit_Description
  ,OrganizationUnit.BranchNumber
  ,OrganizationUnit.BranchNumber
  
FROM
  `....xt_...stock`
  left join unnest(JSON_QUERY_ARRAY(Stock.Sellable)) s
where GenerationTime = timestamp("2022-08-09 01:00:28.169503 UTC")

在此处输入图像描述

Consider below query:

SELECT (SELECT AS STRUCT
               ARRAY_AGG(STRING(s.ProductID) ORDER BY o) AS ProductID,
               ARRAY_AGG(INT64(s.Quantity) ORDER BY o) AS Quantity
          FROM UNNEST(JSON_QUERY_ARRAY(Stock.Sellable)) s WITH OFFSET o
       ) AS sellable
  FROM xt_stock;

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM