ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =2 and op_res.sp = 2),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_pulp_spruce,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =1 and (op_res.sp = 3 or op_res.sp = 4)),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_log_birch,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =2 and (op_res.sp = 3 or op_res.sp = 4)),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_pulp_birch,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and (op_res.assortment =2 or op_res.assortment =1) and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_others,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and (op_res.assortment =2 or op_res.assortment =1) and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_others,
case when t.op_name = "thinning" OR t.op_name = "first_thinning" then t.op_date else 0 end as THIN,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =1 and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_log_others,
ifnull((SELECT sum(op_res.Volume) FROM op_res WHERE t.op_id = op_res.op_id and op_res.assortment =2 and op_res.sp > 4),0)/((select count(*) from (select * from op_res where t.op_id = op_res.op_id) x)/(select count(*) from (select distinct * from op_res where t.op_id = op_res.op_id ) x) ) AS Harvested_V_pulp_others,
sum(Harvested_V_log_pine*0.88 +Harvested_V_log_spruce*0.898 +Harvested_V_log_birch*0.885+Harvested_V_log_others*0.874) as Harvested_V_log_under_bark,
...
...
@@ -99,7 +100,7 @@ create_table_UNIT <- 'Create Table UNIT AS SELECT u.*,
(select sum(stratum.V) From stratum where stratum.data_id = u.data_id and SP = 9) as V_o_decidious,
l.data_date, b.branch, b.branch_desc, b.branching_group, o.income/u.AREA as income, o.cash_flow/u.AREA as cash_flow, o.clearcut, o.Harvested_V_log/u.AREA as Harvested_V_log,
o.Harvested_V_pulp/u.AREA as Harvested_V_pulp, o.Harvested_V/u.AREA as Harvested_V, m.max_v, o.income_log/u.AREA as income_log, o.income_pulp/u.AREA as income_pulp,
o.income_log_change/u.AREA as income_log_change,o.income_pulp_change/u.AREA as income_pulp_change, o.income_biomass, o.Biomass/u.AREA as Biomass,
o.income_log_change/u.AREA as income_log_change,o.income_pulp_change/u.AREA as income_pulp_change, o.income_biomass, o.Biomass/u.AREA as Biomass, o.thin
o.Harvested_biomass_Ton/u.AREA as Biomass_ton,
o.Harvested_V_pulp_under_bark/u.AREA as Harvested_V_pulp_under_bark,