insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'AttributesPerAttributeCollection', rownum, totals, id from 
(select count(1) totals, agn_attr_group_id id
from agn
where agn_company_id = 40001
and agn_next_version_id = 999999999
group by agn_attr_group_id
order by totals desc)
;
commit;
quit;
