Estoy tratando de sumar multiples valores de diferentes tablas, los cuales dependen al final del tipo de gastos que se realizó en las actividades que se registraron en la base de datos.
La consulta puede o no usar un where
para especificar un cultivo, pero cada que la persona vaya a ingresar a la vista, los valores se van a traer sin hacer uso del where.
A continuación colocare la consulta usando el where
y el resultado de esta
select 'act' as type, c.crop_id, f.farm_name, l.lot_name, c.crop_name,
coalesce(c.crop_real_plants::integer, 0) as real_plants, l.lot_area, c.crop_invest,
am.actmach_id, sum(am.actmach_cost)
from crops c
inner join lots l on c.lot_id = l.lot_id
inner join farms f on l.farm_id = f.farm_id
inner join activities a on c.crop_id = a.crop_id
inner join activities_machines am on a.activity_id = am.activity_id
where c.crop_id = 3
group by c.crop_id , f.farm_name , l.lot_name , c.crop_name , l.lot_area , c.crop_invest , am.actmach_id
union
select 'act' as type, c.crop_id, f.farm_name, l.lot_name, c.crop_name,
coalesce(c.crop_real_plants::integer, 0) as real_plants, l.lot_area, c.crop_invest,
ap.actpay_id , sum(ap.actpay_cost)
from crops c
inner join lots l on c.lot_id = l.lot_id
inner join farms f on l.farm_id = f.farm_id
inner join activities a on c.crop_id = a.crop_id
inner join activities_payrolls ap on a.activity_id = ap.actpay_id
where c.crop_id = 3
group by c.crop_id , f.farm_name , l.lot_name , c.crop_name , l.lot_area , c.crop_invest , ap.actpay_id
union
select 'act', c.crop_id, f.farm_name, l.lot_name, c.crop_name,
coalesce(c.crop_real_plants::integer, 0) as real_plants, l.lot_area, c.crop_invest,
asp.actsup_id, sum(asp.actsup_cost)
from crops c
inner join lots l on c.lot_id = l.lot_id
inner join farms f on l.farm_id = f.farm_id
inner join activities a on c.crop_id = a.crop_id
inner join activities_supplies asp on a.activity_id = asp.actsup_id
where c.crop_id = 3
group by c.crop_id , f.farm_name , l.lot_name , c.crop_name , l.lot_area , c.crop_invest , asp.actsup_id
union
select 'cost', c.crop_id, f.farm_name, l.lot_name, c.crop_name,
coalesce(c.crop_real_plants::integer, 0) as real_plants, l.lot_area, c.crop_invest,
cd.cost_detail_id, sum(cd.cost_detail_value)
from crops c
inner join lots l on c.lot_id = l.lot_id
inner join farms f on l.farm_id = f.farm_id
inner join costs_details cd on c.crop_id = cd.crop_id
where c.crop_id = 3
group by c.crop_id , f.farm_name , l.lot_name , c.crop_name , l.lot_area , c.crop_invest , cd.cost_detail_id
union
select 'prod', c.crop_id, f.farm_name, l.lot_name, c.crop_name,
coalesce(c.crop_real_plants::integer, 0) as real_plants, l.lot_area, c.crop_invest,
pc.category_id, sum(pc.category_price)
from crops c
inner join lots l on c.lot_id = l.lot_id
inner join farms f on l.farm_id = f.farm_id
inner join productions p on c.crop_id = p.crop_id
inner join production_categories pc on p.production_id = pc.category_production
where c.crop_id = 3
group by c.crop_id , f.farm_name , l.lot_name , c.crop_name , l.lot_area , c.crop_invest , pc.category_id;
El resultado es el siguiente:
La idea es sumar los valores respetando su tipo. Tomando los valores arrojados por la consulta, debería hacerse de la siguiente forma.
- act
sum(30000 + 6000)
- prod
sum(500000 + 2)
- cost
sum(66666.67 + 133333.33 + 33333.33 + 93333.33)
Luego usar los valores obtenidos en formulas como estas.
- crop_invest / lot_area
- activities_consolidate / crop_real_plants
- activities_consolidate / productions_consolidate