insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'SpecsPerItems', rownum, totals, id from 
(select count(1) totals, items id from
 (select ita_item_id as items, nod_spec_id as specs
from ita, nod
where ita_company_id = 40001
and ita_next_version_id = 999999999
and ita_node_id = nod_id
and nod_company_id = ita_company_id
and nod_next_version_id = ita_next_version_id
group by ita_item_id, nod_spec_id
 ) 
group by items
order by totals desc);
commit;
quit;
