问题
I am having a big query table in this format
I have the record values for the table in this format :
The same Instance Id and project Id may be repeated any no of times with different time series. I am trying for a select statment which will return one value(no cartestian product) for each row
output like
InstanceId ProjectId Time
2763333 manage-x 10:30
2763333 manage-x 11:30
2763334 manage-y 10:30
since this is a record type, I tried this the table name is metric
select res.value from metric,unnest(resource.labels) as res where res.key="instance_id"
This gives me the corect value of 2763339646023081
Now I want to get project_id in the same statement so I need a correlated subquery for that similar to sql
select res.value from metric,unnest(resource.labels) as res,(select proj.value from metric,unnest(resource.labels) as proj where proj.key="project_id" and this part i need help to refer the res.value(instance_id) from the outer query to match to the corresponding instance for the project_id in the inner query)) where res.key="instance_id"
So as shown above I am not sure how to refer the instance_id in the inner subquery to get the corresponding project_id, I am very new to BigQuery and I have tried various combinations of joins yet it does not work and gives me cartestian products. Please give your suggestions and help. Thanks
回答1:
Below is for BigQuery Standard SQL
#standardSQL
SELECT
(SELECT value FROM UNNEST(resource.labels) AS res WHERE key = 'instance_id') AS instance_id,
(SELECT value FROM UNNEST(resource.labels) AS res WHERE key = 'project_id') AS project_id
FROM `project.dataset.metric`
来源:https://stackoverflow.com/questions/62913490/bigquery-need-to-return-unique-values-for-record-type-in-a-correlated-subquery