Puedes usar age()
. Si el valor es siempre inferior a 12 meses, entonces un método es:
SELECT iv.product_name,
extract(month form age('2020-12-20'::date, MAX(iv.inventory_date::date))) AS months_in_inventory
FROM inventory iv
GROUP BY 1
ORDER BY 1;
Un cálculo más preciso tiene en cuenta el año:
SELECT iv.product_name,
(extract(year from age('2020-12-20'::date, MAX(iv.inventory_date::date))) * 12 +
extract(month from age('2020-12-20'::date, MAX(iv.inventory_date::date)))
) AS months_in_inventory
FROM inventory iv
GROUP BY 1
ORDER BY 1;
Aquí es un db<>violín.