I am struggling to achieve this logic in BigQuery. I shall be grateful if someone can help
Consider these 2 CTEs
WITH MaxCycle AS (
SELECT MAX(my_cycle) AS max_cycle
FROM `project_a.dataset_A.table_A`
),
CycleNumbers AS (
SELECT cycle
FROM UNNEST(GENERATE_ARRAY(1, (SELECT max_cycle FROM MaxCycle))) AS cycle
)
Now if I do:
SELECT * FROM CycleNumbers
I get 3 rows with values
cycle
-----
1
2
3
This is perfect
Now, this is what I want:
SELECT
inputs.*
FROM
`project_a.dataset_A.table_B` AS A
LEFT JOIN
`project_a.dataset_A.table_A` AS B
ON
LOWER(A.X) = LOWER(B.X)
AND LOWER(A.Y) = LOWER(B.Y)
AND A.Z = 1 (--note: 1 is the first row of CycleNumbers)
UNION ALL
SELECT
inputs.*
FROM
`project_a.dataset_A.table_B` AS A
LEFT JOIN
`project_a.dataset_A.table_A` AS B
ON
LOWER(A.X) = LOWER(B.X)
AND LOWER(A.Y) = LOWER(B.Y)
AND A.Z = 2 (--note: 2 is the second row of CycleNumbers)
UNION ALL
SELECT
inputs.*
FROM
`project_a.dataset_A.table_B` AS A
LEFT JOIN
`project_a.dataset_A.table_A` AS B
ON
LOWER(A.X) = LOWER(B.X)
AND LOWER(A.Y) = LOWER(B.Y)
AND A.Z = 3 (--note: 3 is the second row of CycleNumbers)
Can someone help me generalize this query in BigQuery?
Effectively I want to loop through each row of CycleNumbers, left join and union
@ms4446 , I shall be grateful if you can take a look at this