问题
I have a data structure like below:
Products
| name | region_ids |
----------------------------------
| shoe | c32, a43, x53 |
| hat | c32, f42 |
# Schema
name STRING NULLABLE
region_ids RECORD REPEATED
region_ids.value STRING NULLABLE
Regions
| _id | name |
---------------------
| c32 | london |
| a43 | manchester |
| x53 | bristol |
| f42 | liverpool |
# Schema
_id STRING NULLABLE
name STRING NULLABLE
I want to look up the array of "region_ids" and replace them by the region name to result in a table like below:
| _id | name | region_names |
----------------------------------------------
| d22 | shoe | london, manchester, bristol |
| t64 | hat | london, liverpool |
What is the best way to do this using standard SQL?
Thanks,
A
回答1:
Below is for BigQuery Standard SQL
#standardSQL
SELECT p._id, p.name,
STRING_AGG(r.name, ', ' ORDER BY OFFSET) AS region_names
FROM `project.dataset.Products` p,
UNNEST(region_ids) WITH OFFSET
LEFT JOIN `project.dataset.Regions` r
ON value = r._id
GROUP BY _id, name
You can test, play with above using sample data from your question as in below example
#standardSQL
WITH `project.dataset.Products` AS (
SELECT 'd22' _id, 'shoe' name, [STRUCT<value STRING>('c32'), STRUCT('a43'), STRUCT('x53')] region_ids UNION ALL
SELECT 't64', 'hat', [STRUCT<value STRING>('c32'), STRUCT('f42')]
), `project.dataset.Regions` AS (
SELECT 'c32' _id, 'london' name UNION ALL
SELECT 'a43', 'manchester' UNION ALL
SELECT 'x53', 'bristol' UNION ALL
SELECT 'f42', 'liverpool'
)
SELECT p._id, p.name,
STRING_AGG(r.name, ', ' ORDER BY OFFSET) AS region_names
FROM `project.dataset.Products` p,
UNNEST(region_ids) WITH OFFSET
LEFT JOIN `project.dataset.Regions` r
ON value = r._id
GROUP BY _id, name
Result is
Row _id name region_names
1 d22 shoe london, manchester, bristol
2 t64 hat london, liverpool
Based on output example in your question - you expect region_names
as string with list of comma separated names
But, if you need region_names
as an array - you can replace STRING_AGG(r.name, ', ' ORDER BY OFFSET)
with ARRAY_AGG(r.name ORDER BY OFFSET)
来源:https://stackoverflow.com/questions/62808554/bigquery-lookup-array-of-ids-type-record-and-join-data-from-secondary-table-usi