select cpp.parent_node_id as planning_parameters_id,
cuh.inventory_item_id as inventory_item_id,
sum(cuh.quantity) as quantity,
cuh.period_start_date as period_start_date,
substr(cpp.level_id,1,instr(cpp.level_id,'.',-1,1) - 1) as level_id
from csp_planning_parameters cpp,
csp_usage_histories cuh
Where cpp.node_type = 'SUBINVENTORY'
and cuh.history_data_type = 1
and cuh.organization_id = cpp.organization_id
and cuh.subinventory_code = cpp.secondary_inventory
group by cpp.parent_node_id,cuh.inventory_item_id,cuh.period_start_date,
substr(cpp.level_id,1,instr(cpp.level_id,'.',-1,1) - 1)