Let's say I have three tables:
table orders:
| invoice_ID | customer_ID |
|---|---|
| 202201 | 1000 |
| 202202 | 2000 |
| 202203 | 3000 |
| 202204 | 4000 |
table department_north
| customer_ID | product | price |
|---|---|---|
| 4000 | VW Rabbit | $5000.00 |
| 1000 | BMW X5 | $15.000 |
table department_south
| customer_ID | product | price |
|---|---|---|
| 3000 | Tesla S | $30.000 |
| 2000 | BMW X3 | $20.000 |
Wanted Result
A table with invoice_id, a new column with all cars that contain '%BMW%', a new column with the attached price
| invoice_ID | product_bmw | price_bmw |
|---|---|---|
| 202201 | BMW X5 | $5.000 |
| 202202 | BMW X3 | $20.000 |
I figured out how to get the results for one department table but can't find a statement for both.
SELECT DISTINCT orders.invoice_ID,
department_north.product AS product_BMW,
department_north.price AS price_BMW
FROM orders
JOIN LEFT department_north
ON department_north.customer_ID = order.customer_id
JOIN LEFT department_south
ON department_south.customer_ID = order.customer_id
WHERE department_north.product LIKE '%BMW%'
OR department_south.product LIKE '%BMW%'orUNIONyour department tables together as one set and join on this as a subquery or CTE, etc.