我有一个PostgreSQL计算
roi
其中的几个值
select 100 * ((SUM(time_product)/SUM(rt_id_count))*SUM(rental_cost))/SUM(sale_count)
使用case语句作为嵌套查询来定义和计算。但是,如果其中任何值为空
rental_cost
它计算
投资回报率
作为
zero
,而这个零值将被卷进平均值中,并向下倾斜平均值。在总计算中,如何将这些零视为空?
(我取出了一些嵌套查询,并用
[calculation]
在这个问题上;主要是因为它是一个很长的查询,我想过滤掉噪音,使整个结构更可见)
(select 100 * ((SUM(time_product)/SUM(rt_id_count))*SUM(rental_cost))/SUM(sale_count) FROM
(select p2.id, p2.inventory_type, p2.working_value, COUNT(distinct p2.id),
----nested values calculated here
(case p2.inventory_type
when 'set' then
[calculation A]
else [calculation B] end) rental_cost,
(case p2.inventory_type
when 'set' then
[caluculation A]
else [calculation B] end) sale_count,
(case p2.inventory_type
when 'set' then count(distinct rt.id)
else 1 end
) rt_id_count,
(case p2.inventory_type
when 'set' then
[calculation ] end) time_total,
[calculation] time_product
FROM warehouses w
LEFT JOIN rfid_tags rt ON w.id = rt.location_id AND rt.location_type = 'Warehouse'
LEFT JOIN products p2 ON rt.ancestor_product_id = p2.id
LEFT JOIN category_assignments ca ON ca.product_id = p2.id
LEFT JOIN categories c ON ca.category_id = c.id
LEFT JOIN product_selections ps ON ps.rfid_tag_id = rt.id
WHERE
c.id=categories.id AND ca.primary = true AND w.id=warehouses.id
AND (select count(ps.id) from product_selections ps where ps.rfid_tag_id=rt.id)>0
and p2.working_value>0
AND rt.location_id=w.id
group by p2.id, p2.inventory_type, p2.working_value, c.sale_price_percentage, c.rental_price_percentage) Z) roi