insert into metadata_statistics(rundate, metadata_defn, order_sequence, total_entries, metadata_id) 
select to_date(sysdate,'YYYY-MM-DD-HH24:MI:SS'), 'AttributesPerCategory', rownum, totals, id from (
with catg_specs as
(select cab_category_id as category, nod_spec_id as specs from cab, nod 
where  cab_company_id = 40001
and cab_next_version_id = 999999999
and cab_node_id = nod_id
and nod_company_id = cab_company_id
and nod_next_version_id = cab_next_version_id
group by cab_category_id, nod_spec_id  
)
select count(1) totals, category id
from nod, catg_specs
where specs = nod_spec_id
and nod_company_id = 40001
and nod_next_version_id = 999999999
and nod_type = 'ATTRIBUTE'
group by category
order by count(1) desc)
;
commit;
quit;
