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 6097483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:02:55+00:00 2026-05-23T13:02:55+00:00

I have a series of calculation times in a DB2 SQL DB that are

  • 0

I have a series of calculation times in a DB2 SQL DB that are stored as float with a default value of 0.0.

The table being updated is as follows:

CREATE TABLE MY_CALC_DATA_TABLE
(
    CALCDATE                 TIMESTAMP,
    INDIV_CALC_DURATION_IN_S FLOAT WITH DEFAULT 0.0,
    CALC_TIME_PERCENTAGE     FLOAT WITH DEFAULT 0.0
)

Using a sproc. I am calculating the sum as follows:

CREATE OR REPLACE PROCEDURE MY_SCHEMA.MY_SPROC (IN P_DATE TIMESTAMP)
    LANGUAGE SQL
    NO EXTERNAL ACTION
BEGIN
    DECLARE V_TOTAL_CALC_TIME_IN_S FLOAT DEFAULT 0.0;

    -- other stuff setting up and joining data

    -- Calculate the total time taken to perform the
    -- individual calculations

    SET V_TOTAL_CALC_TIME_IN_S =
        (
            SELECT
                SUM(C.INDIV_CALC_DURATION_IN_S)
            FROM
                MY_SCHEMA.MY_CALC_DATA_TABLE C
            WHERE
                C.CALCDATE = P_DATE
        )

    -- Now calculate each individual calculation's percentage
    -- of the toal time.

    UPDATE
        MY_SCHEMA.MY_CALC_DATA_TABLE C
    SET
        C.CALC_TIME_PERCENTAGE =
            (C.INDIV_CALC_DURATION_IN_S / V_TOTAL_CALC_TIME_IN_S) * 100
    WHERE
        C.CALCDATE = P_DATE;

END@

Trouble is, when I do a sum of all the CALC_TIME_PERCENTAGE values for the specified CALC_DATE it is always less than 100% with the sum being values like 80% or 70% for different CALC_DATES.

We are talking between 35k and 55k calculations here with the maximum individual calculation’s percentage of the total, as calculated above, being 11% and lots of calculations in the 0.00000N% range.

To calculate the total percentage I am using the simple query:

SELECT
    SUM(C.CALC_TIME_PERCENTAGE)
FROM
    MY_SCHEMA.MY_CALC_DATA_TABLE C
WHERE
    C.CALCDATE = P_DATE;

Any suggestions?

Update: Rearranging the calc. as suggested fixed the problem. Thanks. BTW In DB2 FLOAT and DOUBLE are the same type. And now to read that suggested paper on floats.

  • 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-05-23T13:02:55+00:00Added an answer on May 23, 2026 at 1:02 pm

    If the field C.INDIV_CALC_DURATION_IN_S were Integer, I would assume it’s a rounding error. Reading again, that is not the problem as the datatype is FLOAT.

    You can still try using this. I wouldn’t be surprised if this yielded (slighly) different results than the previous method:

    SET
        C.CALC_TIME_PERCENTAGE =
            (C.INDIV_CALC_DURATION_IN_S * 100.0 / V_TOTAL_CALC_TIME_IN_S)
    

    But you mention that there a lot of rows in a calculation for a certain date, so it may be a rounding error due to that. Try with DOUBLE datatype in both fields (or at least the CALC_TIME_PERCENTAGE field) and see if the difference from 100% gets smaller.

    I’m not sure if DB2 has DECIMAL(x,y) datatype. It may be more appropriate in this case.


    Another problem is how you find the sum of CALC_TIME_PERCENTAGE. I suppose you (and everyone else) would use the:

            SELECT
                P_DATE, SUM(CALC_TIME_PERCENTAGE)
            FROM
                MY_SCHEMA.MY_CALC_DATA_TABLE C
            GROUP BY P_DATE
    

    This way, you have no way to determine in what order the summation will be done. It may not be even possible to determine that but you can try:

            SELECT
                P_DATE, SUM(CALC_TIME_PERCENTAGE)
            FROM
              ( SELECT
                    P_DATE, CALC_TIME_PERCENTAGE
                FROM
                    MY_SCHEMA.MY_CALC_DATA_TABLE C
                ORDER BY P_DATE
                       , CALC_TIME_PERCENTAGE ASC
              ) AS tmp
            GROUP BY P_DATE
    

    The optimizer may disregard the interior ORDER BY but it’s worth a shot.


    Another possibility for this big difference is that rows are deleted from the table between the UPDATE and the SHOW percent SUM operations.

    You can test if that happens by running the calculations (without UPDATE) and summing up:

            SELECT
                P_DATE
              , SUM( INDIV_CALC_DURATION_IN_S * 100.0 / T.TOTAL )                   
                AS PERCENT_SUM
            FROM
                MY_SCHEMA.MY_CALC_DATA_TABLE C
              , ( SELECT SUM(INDIV_CALC_DURATION_IN_S) AS TOTAL
                  FROM MY_SCHEMA.MY_CALC_DATA_TABLE
                ) AS TMP
            GROUP BY P_DATE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of logging breakpoints in Xcode4 that I'm using to selectively
I have a series of divs that slide down using -webkit-transform from a negative
I have a series of images that get loaded for a carousel, and it
I have a series of complex views being displayed on the screen and I
I have a series of grouped values that follow a specific format and would
I have a (C#) genetic program that uses financial time-series data and it's currently
I have a chart control that is displaying columns with values on top (series
I have a Python program that runs a series of experiments, with no data
I have a pandas dataframe consisting of 23 series with a default sequential index
I have a method that contains a series of method calls in it. Some

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.