I have a query that returns customer product information that shows which region a product is sourced from.
Query:
SELECT
prod.prod_id,
prod.is_source,
link.prod_link_disabled,
loc.location_id,
loc.customer_id,
cust.customer_name
FROM
product prod,
product_link link,
region region,
location loc,
customer cust
WHERE
prod.prod_id = link.prod_id
AND link.region_id = region.region_id
AND region.loc_id = loc.loc_id
AND link.prod_link_deleted = FALSE
AND loc.customer_id = customer_id
AND prod.prod_id = 5874;
This returns the following result:
| prod_id | is_source | prod_link_disabled | location_id | customer_id | customer_name |
| ------------------- | --------- | ------------------ | ----------- | ----------- | --------------- |
| Test Product - 5874 | t | f | 2 | 332 | Test Playground |
| Test Product - 5874 | f | f | 1 | 332 | Test Playground |
This is showing that product 5874 is coming from location 2 (because is_source
is true).
I'd like to compress this information into a single row. I'd like a result that looks like this:
| prod_id | prod_link_disabled | src_location_id | dest_location_id | customer_id | customer_name |
| ------------------- | ------------------ | --------------- | ---------------- | ----------- | --------------- |
| Test Product - 5874 | f | 2 | 1 | 332 | Test Playground |
This compressed the two previous rows into a single row where src_location_id
is 2
(again because that row had is_source
as true) and dest_location_id
is 1
How can I pivot the two row result into a single row?
It is safe to assume that there are always only two results in the first query.
Best Answer
Do I understand correctly and you need something like:
Sure, you can remove excessive JOINs.