Three recipes are stored in the tables recipe1, recipe2 and recipe3. Each row is one ingredient line with its mass in grams. Combine the three recipes into a single shopping list that returns each unique ingredient and its total mass summed across all three tables. Duplicate rows within a single table are also summed, and ingredient names are matched case-insensitively.
Input Table(s)
Tables recipe1, recipe2 and recipe3 share the same schema:
| Column | Type | Meaning |
|---|
| ingredient | VARCHAR(100) | Ingredient name. Spelling may differ in case between rows, for example Flour and flour. |
| mass | DECIMAL(10,2) | Mass of that ingredient line in grams, 0 or more. |
None of the tables has a primary key. The same row can appear more than once in one table, and the same ingredient can appear in one, two or all three tables.
Output Contract
| Column | Type | Meaning |
|---|
| ingredient | VARCHAR | The ingredient name in lowercase. |
| total_mass | DECIMAL | Sum of mass over every row of all three tables whose name matches this ingredient, ignoring case. |
One row per ingredient. Order the rows by ingredient ascending. The lowercase names are unique, so this ordering is unique. total_mass keeps the two decimals of the input and needs no extra rounding.
Example
recipe1:
| ingredient | mass |
|---|
| Flour | 500.00 |
| Sugar | 100.00 |
| eggs | 120.00 |
recipe2:
| ingredient | mass |
|---|
| flour | 250.00 |
| Butter | 200.00 |
| Butter | 200.00 |
recipe3:
| ingredient | mass |
|---|
| SUGAR | 50.50 |
| Butter | 200.00 |
| Salt | 5.00 |
Expected output:
| ingredient | total_mass |
|---|
| butter | 600.00 |
| eggs | 120.00 |
| flour | 750.00 |
| salt | 5.00 |
| sugar | 150.50 |
Butter, 200.00 appears twice in recipe2 and once in recipe3, and all three rows count, so butter totals 600.00. Flour and flour are the same ingredient (500.00 + 250.00), and so are Sugar and SUGAR (100.00 + 50.50). Eggs and salt appear in only one table each.
Constraints and Clarifications
-
An ingredient may appear in one, two or all three tables.
-
Names are compared after converting to lowercase. No other normalization applies, so
olive oil
and
olive oil
(two spaces) are different ingredients.
-
mass
is non-negative.
-
Assume no row has a NULL
ingredient
or a NULL
mass
.
-
Every row counts, including exact duplicates within one table and identical rows in different tables.