New to Bigquery, and googling could not really point me to the solution of the problem.
I am trying to use a where clause in a subquery to filter and pick the latest row for each other row in the main query. In postgres I'd normally do it like this:
SELECT
*
FROM
table_a AS a
LEFT JOIN LATERAL
(
SELECT
score,
CONCAT( "AB", id ) AS id
FROM
table_b AS b
WHERE
id = a.company_id
and
b.date < a.date
ORDER BY
b.date DESC
LIMIT
1
) ON true
WHERE
id LIKE 'AB%'
ORDER BY
createdAt DESC
so this would essentially run the subquery against each row and pick the latest row from table B based on a given row's date from table A.
So if table A would have a row
| id | date |
|---|---|
| 12 | 2021-05-XX |
and table B:
| id | date | value |
|---|---|---|
| 12 | 2022-01-XX | 99 |
| 12 | 2021-02-XX | 98 |
| 12 | 2020-03-XX | 97 |
| 12 | 2019-04-XX | 96 |
It would have joined only the row with 2021-02-XX to table a.
In another example, with
Table A:
| id | date |
|---|---|
| 15 | 2021-01-XX |
Table B:
| id | date | value |
|---|---|---|
| 15 | 2022-01-XX | 99 |
| 15 | 2021-02-XX | 98 |
| 15 | 2020-03-XX | 97 |
| 15 | 2019-04-XX | 96 |
it would join only the row with date: 2020-03-XX, value: 97.
Hope that is clear, not really sure how to write this query to work
Thanks for help!




Idcolumn in Table B, because thoseidvalues are not unique identifiers...