insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'SecondarySpecsPerCategory', rownum, totals, id from 
(select count(1) totals, csm_category_id id
from csm, spa
where csm_spec_id = spa_spec_id
and csm_company_id = spa_company_id
and csm_next_version_id = spa_next_version_id
and csm_company_id = 40001
and csm_next_version_id = 999999999
and spa_value = 'SECONDARY_SPEC'
and spa_name = 'TYPE'
group by csm_category_id
order by totals desc);
commit;
quit;
