tbl_Fuel # Temp1
CommodityID Commodity Fuel_Sector Fuel_Rate Fuel_Min Fuel_EffectiveDate
----------------------------------------------------------------------------------------------------
1 General Cargo OTHERS 38.00 0.00 01/Apr/2019
1 General Cargo GULF+M.EAST 27.00 0.00 01/Apr/2019
1 General Cargo C/D 16.00 0.00 01/Apr/2019
2 Perishable - General OTHERS 7.00 0.00 15/Jun/2015
2 Perishable - General GULF+M.EAST 7.00 0.00 15/Jun/2015
2 Perishable - General C/D 7.00 0.00 15/Jun/2015
tbl_War # Temp2
CommodityID Commodity War_Sector War_Rate War_Min War_EffectiveDate
--------------------------------------------------------------------------------------------
1 General Cargo OTHERS 7.00 0.00 01/Apr/2019
1 General Cargo GULF+M.EAST 5.00 0.00 01/Apr/2019
1 General Cargo C\D 5.00 0.00 01/Apr/2019
1 General Cargo BGW 5.00 0.00 01/Apr/2019
3 Cut Flowers OTHERS 2.00 0.00 15/Jun/2015
3 Cut Flowers GULF+M.EAST 2.00 0.00 15/Jun/2015
tbl_XRay # Temp3
CommodityID Commodity XRay_Sector XRay_Rate XRay_Min XRay_EffectiveDate
--------------------------------------------------------------------------------------------
1 General Cargo ALL 2.00 225.00 16/Nov/2019
9 Pharma & Chemicals ALL 2.00 225.00 01/Jun/2011
Желаемый результат:
CommodityID Commodity | Fuel_Sector | Fuel_Rate | Fuel_Min | Fuel_EffectiveDate |War_Sector | War_Rate | War_Min | War_EffectiveDate | XRay_Sector | XRay_Rate | XRay_Min | XRay_EffectiveDate
1 General Cargo | OTHERS | 38.00 | 0.00 | 01/Apr/2019 | OTHERS | 7.00 | 0.00 | 01/Apr/2019 | ALL | 2.00 | 225.00 | 16/Nov/2019
1 General Cargo | GULF+M.EAST | 27.00 | 0.00 | 01/Apr/2019 | GULF+M.EAST| 5.00 | 0.00 | 01/Apr/2019 | | | |
1 General Cargo | C/D | 16.00 | 0.00 | 01/Apr/2019 | C\D | 5.00 | 0.00 | 01/Apr/2019 | | | |
1 General Cargo | NULL | NULL | NULL | NULL | BGW | 5.00 | 0.00 | 01/Apr/2019 | | | |
2 Perishable - General | OTHERS | 7.00 | 0.00 | 15/Jun/2015 | | | | | | | |
2 Perishable - General | GULF+M.EAST | 7.00 | 0.00 | 15/Jun/2015 | | | | | | | |
2 Perishable - General | C/D | 7.00 | 0.00 | 15/Jun/2015 | | | | | | | |
3 Cut Flowers | | | | |OTHERS | 2.00 | 0.00 | 15/Jun/2015 | | | |
3 Cut Flowers | | | | |GULF+M.EAST | 2.00 | 0.00 | 15/Jun/2015 | | | |
9 Pharma & Chemicals | | | | | | | | | ALL | 2.00 | 225.00 | 01/Jun/2011
После стольких Condn .. Я получаю данные в трех разных временных таблицах ... War, Fuel, XRay ... Я хочу показать с точки зрения данных, если возможно ....
Я пробовал этот запрос:
SELECT *
INTO #Temp4
FROM
(SELECT DISTINCT AirLine, CommodityId FROM #Temp1
UNION ALL
SELECT DISTINCT AirLine, CommodityId FROM #Temp2
UNION ALL
SELECT DISTINCT AirLine, CommodityId FROM #Temp3) a
SELECT *
FROM #Temp4
SELECT *
FROM #Temp4 a1
LEFT OUTER JOIN #Temp1 a2 ON a1.AirLine = a2.AirLine
AND a1.CommodityId = a2.CommodityId
LEFT OUTER JOIN #Temp2 a3 ON a1.AirLine = a3.AirLine
AND a1.CommodityId = a3.CommodityId
LEFT OUTER JOIN #Temp3 a4 ON a1.AirLine = a4.AirLine
AND a1.CommodityId = a4.CommodityId
Этот запрос сначала DISTINCT хранит данные в другой временной таблице, затем я делаю несколько левых соединений со всеми временными таблицами, но он не возвращает правильный вывод.