Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (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
Add Comment
Please, Sign In to add comment