running a loop in oracle

I am new to oracle,

I have the code below

select base_station_key,date_key, time_key, sum(revenue_amt) as revenues, sum(mou) as mou from (
                 select call_duration_qty as mou, time_key, date_key, base_station_key, revenue_amt from bib_cdr.FCT_cdr_rated_sumd  where dw_subpart=0
                 and date_key between 20150101 and 20150331 and package_key in (21395,22102,21448,18998) and usage_type_key=41

That code runs fast because it is partitioned on dw_subpart, there are 10 partitions and that means i have to run 10 times the same code. I was thinking of using a loop and create temporary tables which i can put together later.

Thank you

Anthony Matovu
Anthony MatovuBusiness Analyst, MTN UgandaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Do you think you can use UNION clause to join all the selected results? and when necessary, you can put this result into a temporary table.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnsoneSenior Oracle DBACommented:
I would not use UNION, I would use UNION ALL.  UNION would generate an unnecessary sort.

Without all the details it is tough to give a recommendation, but you should be able to do it all in one query.
slightwv (䄆 Netminder) Commented:
I agree, the query posted doesn't provide much detail.  For example:
What is the table partitioned on?
What indexes are on the table?
Are they local or global?

In Oracle temporary tables should be considered a last resort and when used they should be Global Temporary Tables.  What those are are in the online docs...

You mention it run fast because of partitions.  I fail to see why you need 10 queries for the 10 partitions.  Just run it against the table and not the individual partition!

If the partitions are indexed properly you shouldn't notice any difference over 10 individual queries.  Run an explain plan on the two.  The optimizer might be smart enough to make them the same.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.