Code:food2.sql
Like Code:food1.sql, this analyzes food production data from FAO, and nutrition data from USDA. But while food1 is focused on plants (crops primary), food2 is focused on animal products (livestock primary).
Dependencies:
-- Analysis of the production of animal products
-- Load data from various sources
.mode csv
-- cross-reference table 2: livestock products (FAO data) with foods (USDA nutrition data):
.import fao2usda.csv fao2usda
-- nutrition (USDA):
.import data/sr28-collated.csv usda
-- crop production (FAO):
.import data/fao-livestock-primary.csv animal
.mode list
CREATE TABLE summary AS
SELECT `Item`,
food_grams_per_day_per_capita,
food_grams_per_day_per_capita * 0.01*`Energy (kcal)` AS kalories_per_day_per_capita,
food_grams_per_day_per_capita * 0.01*`Protein (g)` AS protein_grams_per_day_per_capita,
food_grams_per_day_per_capita * 0.01*`Total lipid (fat) (g)` AS fat_grams_per_day_per_capita
FROM (
SELECT *,
AVG(`Value`) -- tonnes per year
* 2737.9093 -- convert to grams per day
/ 7.9e9 -- per capita, world population
--* 0.01*(100-`Refuse`)*`Multiplier` -- edible portion, but this is commented out because actually FAO already specifies that the data is for edible portion, and so does USDA
AS food_grams_per_day_per_capita
FROM animal
JOIN fao2usda USING (`Item Code (FAO)`)
JOIN usda USING (`NDB_No`)
WHERE `Area`="World" AND `Element`="Production" AND `Unit`="tonnes"
GROUP BY `Item`
);
-- Total animal protein and kalories
SELECT SUM(protein_grams_per_day_per_capita) FROM summary;
SELECT SUM(kalories_per_day_per_capita) FROM summary;
-------------------------------
----- DATA & CODE TESTING -----
-- Test that all items have "Production" in tonnes/year (both queries should return the same number)
SELECT COUNT(DISTINCT `Item`) FROM animal;
SELECT COUNT(DISTINCT `Item`) FROM animal WHERE `Area`="World" AND `Element`="Production" AND `Unit`="tonnes";