Prøv
SELECT SUM(f.qty) used_times,
COALESCE(i.ingr_code, f.food_code) code,
COALESCE(i.name, f.name) name,
SUM(COALESCE(i.qty, 1) * f.qty) qty,
COALESCE(i.meas, f.meas) meas
FROM usedfood f LEFT JOIN ingredients i
ON f.food_code = i.food_code
GROUP BY i.ingr_code, i.name
Output:
| USED_TIMES | KODE | NAVN | ANTAL | MÅL |------------------------------------------------ ----| 2 | 173 | ketchup | 2 | stk || 2 | 1130 | corned beef | 80 | gram || 2 | 1135 | laurbær | 0,8 | gram || 2 | 1136 | nelliker | 2 | gram || 2 | 1138 | tomat | 160 | gram || 3 | 1139 | sennep | 15 | gram || 3 | 1140 | frisk æg | 150 | gram || 8 | 1144 | salt | 3.4 | gram || 5 | 1256 | spaghetti rinf | 375 | gram || 8 | 1258 | olie | 362 | gram |
Her er SQLFiddle demo