P粉4760461652023-08-14 10:06:04
You can use case when
SELECT t2.item_id, t2.item_name, t2.menu_id, t1.menu_name, MAX(CASE WHEN t3.currency_code = 'EUR' THEN t3.price END) AS p_EUR, MAX(CASE WHEN t3.currency_code = 'USD' THEN t3.price END) AS p_USD, MAX(CASE WHEN t3.currency_code = 'GBP' THEN t3.price END) AS p_GBP, MAX(CASE WHEN t3.currency_code = 'RON' THEN t3.price END) AS p_RON, MAX(CASE WHEN t3.currency_code = 'LEV' THEN t3.price END) AS p_LEV FROM table_2 AS t2 JOIN table_1 AS t1 ON t2.menu_id = t1.menu_id LEFT JOIN table_3 AS t3 ON t2.item_id = t3.item_id GROUP BY t2.item_id, t2.item_name, t2.menu_id, t1.menu_name;
If you need to convert null to 0, you can use COALESCE
as shown below
COALESCE(MAX(CASE WHEN t3.currency_code = 'LEV' THEN t3.price END), 0) AS p_LEV