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'); Expected Result:
product_name | months_in_inventory | --------------------|----------------------------|---------- Product_A | 2 | Product_B | 1 | Product_C | 1 | Product_D | 0 | I want to calculate the months_in_inventory by calculating the difference between a fixed_date and the inventory_date.
In the example the fixed_date is '2020-12-20' and I am using it my query.
In postgresSQL I was able to achieve the expected result with query from this question:
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; However, when I run this query on redshift I get error:
ERROR: Specified types or functions (one per INFO message) not supported on Redshift tables. How do I need to change the query to make it work on redshift?