I’m working with two common table expressions and trying to calculate a ratio between values from each CTE. The division keeps returning 0 even though both numbers are positive integers.
WITH
sales_data AS (
SELECT item_id, sum(sold_quantity) as total_sold
FROM sales_records
GROUP BY item_id
),
inventory_data AS (
SELECT item_id, available_stock
FROM warehouse_inventory
)
SELECT
inv.item_id,
inv.available_stock,
sales.total_sold,
sales.total_sold/inv.available_stock AS ratio
FROM inventory_data inv
JOIN sales_data sales ON inv.item_id = sales.item_id
The query returns correct values for available_stock and total_sold columns, but the division always shows 0 instead of the expected decimal result. What could cause this behavior?