insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'EntriesPerLookupTable', rownum, totals, id from 
(select count(1) totals, ctg_id id
from ctg,itm
where ctg_id = itm_container_id
and ctg_company_id = itm_company_id
and ctg_company_id = 40001
and ctg_type = 'LOOKUP_TABLE'
and itm_next_version_id = 999999999
group by ctg_id
order by totals desc);
commit;
quit;
