Lakesh:
Je voudrais me joindre à trois tables dans SQL. Tableau 1 et les besoins de Table2 à joindre sur le symbole clé, mais le symbole de table1 besoin modifié avant de rejoindre.
Trois rejoint:
SELECT table1.symbol, table1.risk_factor, table1.risk_factor_name, table1.type, table1.position, table3.`Position dif ex corr`, table1.haircut
FROM `Positions_EOD` table1
LEFT JOIN `positions1` table2 ON table1.symbol = table2.symbol
LEFT JOIN (SELECT * FROM `Final_Pos` WHERE business_date = '2020-04-01' AND `Position check` = 'Correct') table3 ON table2.isin = LEFT(table3.VT_id, INSTR(table3.VT_id, '_') - 1)
WHERE table1.business_date = '2020-04-01' AND table2.business_date = '2020-04-01' AND table2.legal_entity = 'HK' AND table1.symbol LIKE 'ES%'
Avant de se joindre table1, il doit être modifié comme suit:
SELECT
CASE
WHEN (`type` = 'ST') THEN symbol
WHEN (`type` = 'FU') THEN LEFT(REPLACE(symbol,' ', ''), LENGTH(REPLACE(symbol,' ', ''))-2)
END AS 'symbol'
FROM `Positions_EOD`
Comment puis-je modifier les table1 rejoindre pour ce symbole de table1 est édité avant de rejoindre?
passeront:
Utilisez cette expression CASE dans la clause ON:
SELECT table1.symbol, table1.risk_factor, table1.risk_factor_name, table1.type, table1.position, table3.`Position dif ex corr`, table1.haircut
FROM `Positions_EOD` table1 LEFT JOIN `positions1` table2
ON CASE
WHEN (table1.`type` = 'ST') THEN table1.symbol
WHEN (table1.`type` = 'FU') THEN LEFT(REPLACE(table1.symbol,' ', ''), LENGTH(REPLACE(table1.symbol,' ', ''))-2)
END = table2.symbol
..................................