insert into metadata_statistics (order_sequence, metadata_defn, total_entries, rundate, metadata_id)
select rownumber() over (order by count(1) desc), 'AttributesPerSecondarySpec', 
count(1), current timestamp, noh_spec_id
from noh, spa
where noh_spec_id = spa_spec_id
and noh_company_id = spa_company_id
and noh_next_version_id = spa_next_version_id
and noh_company_id = 40001
and noh_next_version_id = 999999999
and spa_name = 'TYPE'
and spa_value = 'SECONDARY_SPEC'
and noh_child_node_id in
(select nod_id from nod
where NOD_COMPANY_ID = noh_company_id
and NOD_NEXT_VERSION_ID = noh_next_version_id
and nod_type = 'ATTRIBUTE'
) 
group by noh_spec_id
;
commit;
