0

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:

result using where

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

1 respuesta 1

0

Englobando toda tu query actual como un resultado nombrado "table_output", puedes usar la función SUM() y ORDER BY type para obtener el sumatorio de todos los registros de cada tipo:

SELECT table_output.type, table_output.crop_invest, table_output.lot_area FROM (
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) AS table_output ORDER BY type;

Donde ahora tienes el crop_invest y lot_area deberías indicar todas las columnas con las que posteriormente querrás operar para hacer las divisiones.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.