Skip to content
Advertisement

BigQuery: JOIN on single matching row

I have two tables, one containing orders with a nested line_items structure and another with a pricing history for each product sku code.

Orders Table

order_id order_date item_sku item_quantity item_subtotal
1 2022-23-07 SKU1 7 12.34
SKU2 1 9.99
2 2022-12-07 SKU1 1 1.12
SKU3 5 32.54

Price History Table

item_sku effective_date cost
SKU1 2022-20-07 0.78
SKU2 2022-02-03 4.50
SKU1 2022-02-03 0.56
SKU3 2022-02-03 4.32

Desired Output

order_id order_date item_sku item_quantity item_subtotal cost
1 2022-23-07 SKU1 7 12.34 0.78
SKU2 1 9.99 4.50
2 2022-12-07 SKU1 1 1.12 0.56
SKU3 5 32.54 4.32

I’m trying to get the product cost by finding the cost at the time of the order being placed.

The above query works but creates a separate line_item array row for each record in the price history table.

How can I match on just the most recent price for that sku. I want to add something like this

ORDER BY effective_date DESC LIMIT 1

But can’t work out how to add it.

Advertisement

Answer

How can I match on just the most recent price for that sku

You need to add below line into subquery and move join out of select to address correlated subquery issue

so, the final query will look like below

with output

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement