Calculate emissions for a quantity

Numbers checked against the live data on

This page shows queries.

Emissions are the quantity times the factor's default value, in kg CO2e, and only when your quantity is in the factor's unit_code. Burning 500 litres of butane with the DESNZ combustion factor gives 872.665 kg CO2e.

The query#

Pick the factor by slug, keep its default row, and put the unit check inside the multiplication so a mismatch returns NULL instead of a wrong number.

Show the SQL
SELECT slug,
       unit_code,
       value_co2e_native,
       gwp_method_native,
       CASE WHEN unit_code = 'l'
            THEN round((500 * value_co2e_native)::numeric, 3)
       END AS kg_co2e
FROM open_ef.factors_flat
WHERE slug = 'ef-desnz-gaseous_fuels_butane-gb-l-combustion-d74d3fe6'
  AND is_default_indicator
  AND is_latest_activity_year;
slug unit_code value_co2e_native gwp_method_native kg_co2e
ef-desnz-gaseous_fuels_butane-gb-l-combustion-d74d3fe6 l 1.74533 ar5 872.665

The value is kg CO2e per unit_code, so 500 × 1.74533 = 872.665 kg CO2e.

Why it works#

Why it works. Three column rules make this safe. is_default_indicator AND is_latest_activity_year keeps exactly one row: a factor can carry several (other years, other GWP vintages), and multiplying all of them multiplies your emissions. unit_code is the denominator of the value, so the CASE returns NULL when your quantity is in another unit. NULL means "not calculated", never zero: convert the quantity first, or pick the factor published in your unit.

Watch out. value_co2e_native is on the publisher's own GWP vintage (gwp_method_native, here AR5). That is right for a single line. If you will add this line to lines from other libraries, use value_co2e_normalized and follow Add up factors across libraries.

This butane factor covers combustion only. Its upstream part is a separate factor: see Sum a decomposition before you add the two.