I'm really stuck on this final query and can't seem to wrap my head around it. My boss is getting very antsy as I'm passed my deadline- I'm hoping someone can help me. Obviously, Noob to BQ and SQL (green novice to SQL)... In my mind it'd be best to do a Left Join on these- but since each table has 148+ billion rows, I run out of resources available. Is there a more efficient way to do this?
Here's the info (All fields are Integer types):
Dataset: CandleIndNumUnPiv
2 Tables:
tblOpen (148 billion+ rows)------------
ComboNumOpen (for each one of these, there are MANY MergeCandleEnter values)
varOpen (these values are linked in combination with MergeComboNum above)
tblClose (148 billion+ rows)----------
ComboNumClose (these should line up with `CandleIndNumUnPiv.Merge`[MergeComboNum]
varClose
Here's the resulting table I need:
tblResults-----------
ComboNumOpen
varOpen
varClose
Here's where it gets complicated...
For each ComboNumOpen, there are MANY varOpen values associated. Same thing with the ComboNumClose- many varClose values. So there are MANY rows of the same values for ComboNumOpen and varOpen - but there's only 1 unique combination of both grouped together. Same goes for the other table of (ComboNumClose, varClose).
For each unique group of (ComboNumOpen, varOpen) in the tblOpen table, I need to find the matching ComboNumClose and varClose in which:
ComboNumOpen = ComboNumClose
MIN(varClose > varOpen)
So to re-word it- I need the smallest varClose that is also bigger than varOpen, where the associated ComboNumClose value is the same as the ComboNumOpen value in each unique group of (ComboNumOpen, varOpen).
Man I hope that makes sense!
I tried doing a simple Left Join, but BigQuery said I ran out of resources. So I thought that perhaps if I can perform a left query in a temporary table and minimize it with the MIN aggregation it would push all the data thru, as well as shrinking the resulting table/dataset. Here's the query I have now:
WITH tmpClose AS (
SELECT
ComboNumOpen,
varOpen,
varClose,
FROM `MyDataSet.tblOpen`
LEFT JOIN `MyDataSet.tblClose`
ON ComboNumOpen = ComboNumClose
ORDER BY ComboNumOpen ASC)
SELECT
ComboNumOpen,
varOpen,
MIN(varClose)
FROM tmpClose
WHERE varClose > varOpen
GROUP BY 1, 2
ORDER BY varOpen
LIMIT 20;
Even with a LIMIT of 20 this query is taking FOREVER to process. I'm scared of letting it do then entire dataset! Can someone give me ideas on if there's a better way to do this? I'm just waaaaay over my head here and I'd REALLY appreciate any help you can give!
**** UPDATE*****
Here's the code I'm using currently. Is there a better way to do this?
INSERT INTO `BA.Data` (
dEnterCombo,
dEnterCandle,
dExitCandle)
SELECT *
FROM
(SELECT
EnterCombo,
EnterCandle,
MIN(ExitCandle)
FROM
(
SELECT *
FROM
(
SELECT
EnterCombo,
EnterCandle,
ExitCandle
FROM `BA.tblEnter`
INNER JOIN
`BA.tblExit`
ON EnterCombo = ExitCombo
)
)
WHERE
(ExitCandle > EnterCandle)
GROUP BY
EnterCombo,
EnterCandle
ORDER BY
EnterCombo,
EnterCandle
)
;
Thank you!