insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'CategoryTreesPerCatalog', rownum, totals, id from 
(select count(1) totals, caa_catalog_id id
from caa
where caa_name = 'CATEGORY_TREE_ID'
and caa_next_version_id = 999999999
group by caa_catalog_id
order by count(1) desc);
commit;
quit;
