Query Assistance - Many to Many Join
Hi All,
I hope that I am posting this in the correct section. I need some help with creating a query. My current query seems to result in a many to many join, returning over 100,000,000 rows, instead of only 550 or so.
The scenario is as follows:
All the source data is populated into a master table, with multiple rows being created for each record.
The multiple rows is as a result of multiple questions being asked, and each question creates a new row in the master table.
Each record set is link by a record number.
My query looks like the below:
SELECT
SEG.record_number
, SEG.action_date
, SEG.location
, CASE
WHEN SEG.headers_question = 'Segmentation' THEN SEG.headers_value
END 'Segmentation'
, CASE
WHEN OUTL.headers_question = 'Address' THEN OUTL.headers_value
END 'Address'
, CASE
WHEN TOW.headers_question = 'AREA' THEN TOW.headers_value
END 'Area'
, CASE
WHEN GPS.headers_question = 'GPS coordinates' THEN GPS.headers_value
END 'GPS_Location'
FROM db.outletdata SEG
JOIN
db.outletdata OUTL
ON SEG.record_number = OUTL.record_number
JOIN
db.outletdata TOW
ON SEG.record_number = TOW.record_number
JOIN
db.outletdata GPS
ON SEG.record_number = GPS.record_number;
Your help / guidance will be highly appreciated.