vshah
Lifer
So, i've got the following tables:
each index in the constituents table may have an entry in the configuration table.
.HSI has a top_n_limit of 15
.KS200 has a top_n_limit of 10
What i'm trying to do is select the index, the top_n_limit, and the weight of the top_N constituents in one query.
starts out easy:
this gets me the index and the top_N number.
I can easily get the top N weight for a single index with the following:
however i'm having trouble integrating the two so that I get one query with index, top_N_limit, and the top N weight as calculated in the 2nd query.
the problem is that I have to use the inner query to select and order the index constituents, such that when I use rownum in the outer query, I'll get only the top-N constituents, ordered by weight. I'm having trouble expanding this to support multiple indices.
i'm kinda a sql noob beyond the basics....any insight on how to expand the 2nd query to multiple indices, while still summing only the top-N weights for each index would be much appreciated. Note that the N can be different for each index, so that's why there's a select after the rownum.
Code:
index_constituents(indices, constituents, weights)
idx_ric | const_ric | weight
index_configuration (indices, top_N value)
idx_ric | top_n_limit
.HSI has a top_n_limit of 15
.KS200 has a top_n_limit of 10
What i'm trying to do is select the index, the top_n_limit, and the weight of the top_N constituents in one query.
starts out easy:
Code:
select idx_ric, top_n_limit
from index_configuration,
where ric in ('.KS200','.HSI')
and top_n_limit > 0;
I can easily get the top N weight for a single index with the following:
Code:
select idx_ric sum(weight) weight from
(select idx_ric, const_ric, weight from index_constituents where idx_ric = '.HSI'
order by idx_ric, weight desc)
where rownum < (select top_n_limit from index_configuration where ric = '.HSI') + 1
group by idx_ric;
the problem is that I have to use the inner query to select and order the index constituents, such that when I use rownum in the outer query, I'll get only the top-N constituents, ordered by weight. I'm having trouble expanding this to support multiple indices.
i'm kinda a sql noob beyond the basics....any insight on how to expand the 2nd query to multiple indices, while still summing only the top-N weights for each index would be much appreciated. Note that the N can be different for each index, so that's why there's a select after the rownum.