insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'DepthPerHierarchy', rownum, totals, id from 
(select to_number(max(csa_value)) totals, csa_cat_tree_id id
from csa
where csa_next_version_id = 999999999  
 and  csa_company_id = 40001 
 and  csa_name = 'LEVEL'
group by csa_cat_tree_id
order by totals desc);
commit;
quit;
