insert into metadata_statistics (order_sequence, metadata_defn, total_entries, rundate, metadata_id)
select rownumber() over (order by integer(max(csa_value)) desc), 
'DepthPerHierarchy', integer(max(csa_value)),
current timestamp, csa_cat_tree_id
from csa
where csa_next_version_id = 999999999  
 and  csa_company_id = 40001 
 and  csa_name = 'LEVEL'
group by csa_cat_tree_id
;
commit;
