title: Calculate exact month-difference between two dates
body: <p><strong><a href="https://dbfiddle.uk/?rdbms=postgres_13&amp;fiddle=772b749d5314317e74e1c807b01f63f5" rel="nofollow noreferrer">DB-Fiddle</a></strong></p>
  <pre><code>CREATE TABLE inventory 
  (
      id SERIAL PRIMARY KEY,
      inventory_date DATE,
      product_name VARCHAR(255),
      product_value VARCHAR(255)
  );
  
  INSERT INTO inventory (inventory_date, product_name, product_value)
  VALUES ('2020-10-19', 'Product_A', '400'),
         ('2020-10-22', 'Product_B', '400'),
         ('2020-11-20', 'Product_C', '900'),
         ('2020-11-25', 'Product_D', '300');
  </code></pre>
  <p>Expected result:</p>
  <pre><code>    product_name | months_in_inventory
      -------------+--------------------
      Product_A    |       2
      Product_B    |       1
      Product_C    |       1
      Product_D    |       0
  </code></pre>
  <p>I want to calculate the <code>months_in_inventory</code> by calculating the difference between a <code>fixed_date</code> and the <code>inventory_date</code>.</p>
  <p>In the example the <code>fixed_date</code> is  <code>'2020-12-20'</code> and I am using it my query.</p>
  <p>So far I am able to calculate the difference in days:</p>
  <pre><code>SELECT
      iv.product_name,
      '2020-12-20'::date - MAX(iv.inventory_date::date) AS days_in_inventory
  FROM 
      inventory iv
  GROUP BY 
      1
  ORDER BY 
      1;
  </code></pre>
  <p>However, I could not figure out how to change it to a difference in month. Do you have any idea?</p>
  <p><strong>NOTE</strong></p>
  <p>I know that one way to approach this would be extracting the <code>month</code> from the <code>fixed_date</code> and <code>inventory_date</code> and subtract both numbers. However, this would not give me the correct result because I need it exactly based on the dates.</p>
  <p>For example <code>Product_B</code> is only <code>1 month</code> in inventory because <code>2020-10-22</code> is not two months compared to <code>2020-12-20</code>.</p>