Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:05:47+00:00 2026-06-12T00:05:47+00:00

I have two tables Quantity(user, year, quantity) and MonthlyQuantity(user, year, month, quantity) – the

  • 0

I have two tables Quantity(user, year, quantity) and MonthlyQuantity(user, year, month, quantity) – the monthly one is empty.
What i need to do is to fill the monthly table based on the yearly one, by dividing the yearly qty in 12 equal parts, and adding any rest to the last month. So basically, each row in Quantity should create 12 rows in QuantityMonthly.
Here’s how i do it – the problem is that the loop is very slow.

How could i do it faster?

    create ro replace 
procedure pr_test
AS
BEGIN
    FOR r IN (SELECT * FROM Quantity) LOOP

    DELETE FROM QuantityMonthly qm WHERE qm.company = r.company AND qm.year = r.year;

    INSERT ALL
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 1, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 2, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 3, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 4, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 5, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 6, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 7, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 8, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 9, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 10, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 11, trunc(r.quantity / 12))
    INTO QuantityMonthly (company, year, mon, qty) values (r.company, r.year, 12, trunc(r.quantity / 12) + mod(r.quantity , 12))
  SELECT * FROM dual;

END LOOP;

COMMIT;

END pr_test;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:05:48+00:00Added an answer on June 12, 2026 at 12:05 am

    Use a single statement, a single set operation is often much faster than many small operations:

    MERGE INTO QuantityMonthly qm
     USING (SELECT *
              FROM Quantity q
             CROSS JOIN (SELECT rownum mon
                           FROM dual
                        CONNECT BY level <= 12)) q
        ON (qm.company = q.company
            AND qm.year = q.year
            AND qm.mon = q.mon)
    WHEN MATCHED THEN
       UPDATE SET qm.quantity = q.quantity / 12 
                                + CASE WHEN q.mon = 12 THEN 
                                     mod(r.quantity, 12) 
                                  ELSE 
                                     0 
                                  END
    WHEN NOT MATCHED THEN
       INSERT (company, year, mon, qty)
       VALUES (q.company, q.year, q.mon,
               q.quantity / 12 
               + CASE WHEN q.mon = 12 THEN mod(r.quantity, 12) ELSE 0 END);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables connected with one to many relationship. Parent Table is a
I have two mySQL tables as follows: [product] table P_id | Name | Quantity
I have two tables, one stores the products and quantity we have bought, the
I have some data from two tables: Table prod_schedule Line Model Lot_no Quantity Lot_Quantity
I have two tables packages table: packageid, quantity transactions table: transactionid, packageid, quantity Whenever
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables in my MySQL database, one is a library of all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.