Google-sheets – LEFT JOIN two and more tables in Google Sheets

formulasgoogle sheetsgoogle-sheets-arrayformulagoogle-sheets-custom-functiongoogle-sheets-query

Say I have two tables. Table A (material requirements):

product_id | material | required
1          | A        | 0.5
1          | B        | 0.7
2          | A        | 0.2
3          | A        | 0.12

And Table B (orders):

order_id | product_id | quantity
1        | 1          | 100
2        | 2          | 10

I want to do a LEFT JOIN* on these two tables (table B being outer), such that the result would be:

order_id | product_id | quantity | material | required
1        | 1          | 100      | A        | 0.5
1        | 1          | 100      | B        | 0.7
2        | 2          | 10       | A        | 0.2

How do I do this? Of course, the end goal is to multiply the required with the quantity

Ideally, there should be an additional table with just basic product information, e.g. something like:

product_id | name | description

*It should be LEFT JOIN, NOT INNER JOIN simply because all orders should be shown, even if the info in product material description is not yet input. An order cannot be 'forgotten' because inner join is used.

Best Answer

  • one-cell solution (paste in D1):

={"material", "required", "total";
 ARRAYFORMULA(IFERROR(VLOOKUP(B2:B, Sheet2!A2:C, {2,3}, 0), )),
 ARRAYFORMULA(IF(LEN(A2:A), C2:C*VLOOKUP(B2:B, Sheet2!A2:C, 3, 0), ))}

  • if you want it to export into Sheet3 use:

={{QUERY(Sheet1!A1:C, "select *", 1)},
  {"material", "required", "total";
  ARRAYFORMULA(IFERROR(VLOOKUP(Sheet1!B2:B, Sheet2!A2:C, {2,3}, 0), )),
  ARRAYFORMULA(IF(LEN(Sheet1!A2:A), Sheet1!C2:C*VLOOKUP(Sheet1!B2:B, Sheet2!A2:C, 3, 0), ))}}

enter image description here