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

  • Home
  • SEARCH
  • 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 6119935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:36:37+00:00 2026-05-23T15:36:37+00:00

Baseline Table ProjectId ProjectName Forecast ——— ———– ——– 11258 Test Proj1 0.678 11259 Test

  • 0
Baseline Table

ProjectId   ProjectName    Forecast
---------   -----------    --------
  11258      Test Proj1      0.678 
  11259      Test Proj2      2.57 

FundEntity Baseline Table

ProjectId    FundEntityId    Forecast    ForecastDollars
---------    -----------     --------    ---------------
  11258          5             0.226
  11258          8             0.226
  11258          11            0.226 

I have a stored procedure that loops through each entry in the ProjectSummaryEntity Table (Yuck!) and for each one, break the forecast down based on “allocations” set in the FundEntity table. Then, I need to calculate the dollar amount of the forecast based on a certain rate in a “Rate” table. The rate is determined by the FundEntity as well.

The stored procedure is running a bit slow (14s for 6.6k rows) and I’m not surprised due to inefficiency of code. My question is, how do I do “on the fly” calculations without using cursors?

The reason why I use cursors is that:

  • forecast amount is broken down first (from ProjectSummary), saved to a variable
  • rate is determined, saved to variable
  • dollar amount is determined, saved to a variable
  • final update statement is run to update forecast and forecast dollars

If I don’t use cursors there’s no way to update the dollars based on the forecast. Any help is appreciated. Thanks!

EDIT: The above is a really simplified version of the whole thing. The stored procedure that deals with this is in: http://pastebin.ubuntu.com/629888/

  • 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-23T15:36:38+00:00Added an answer on May 23, 2026 at 3:36 pm

    Note that the Stored Procedure will run slow not because of your cursor, but because of the subqueries you need to run per row (11 selects + 1 update). Just by combining some of those queries you should be able to significantly improve performance (e.g. the calculations for forHcm, budgetHcm and revPlanHcm could easily be joined into one query as they all are calculations on Baseline table columns).

    To answer your specific cursor question, it is possible to remove the cursor as you don’t need to generate row output values based on output values for other rows (e.g. if the value of hcmRate for one fundEntityBaselineId was calculated using hcmRate values for another FundEntityBaselineId values, then it would be very difficult to avoid the cursor).
    The final query without cursor though will be a little complex.

    Taking your stored procedure at http://pastebin.ubuntu.com/629888/ and assuming that if you use COALESCE I need an OUTER JOIN (no COALESCE then INNER JOIN), here is what your single query should look like (to fix the sections marked “refactor” I would need to know more about your tables):

    SELECT fblId,
           ForecastAmount * fundAlloc AS foreHCM
           hcmRate,
           RE_Value * fundAlloc AS reqForeHcm,
           PlanAmount * fundAlloc AS budgetHcm
           RevisedPlanAmount * fundAlloc AS revPlanHcm,
           actualForHcm_pre * fundAlloc AS actualForeCastHcm
    FROM
    (SELECT b.FundEntityBaselineId AS fblId,
            COALESCE(fa.fundAllocation,100)/100 as fundAlloc,
            COALESCE(hcmRate1.Usd_Rate,hcmRate2.HCMRate) as hcmRate,
            bl.ForecastAmount,
            COALESCE(re.Value,0) as RE_Value,
            planHcmRate.BudgetHcmRate as planHcmRate,
            bl.PlanAmount,
            bl.RevisedPlanAmount,
            COALESCE(afp_p.actualForHcm_pre,0) as actualForHcm_pre
            FROM FundEntityBaseline b
              INNER JOIN ProjectDetail pd ON pd.ProjectId = b.fblProjectId
              INNER JOIN Baseline bl on bl.BaselineId = b.BaselineId
              INNER JOIN ProjectTeam pt ON bl.ProjectTeamId = pt.ProjectTeamId
            INNER JOIN SiteTeam st ON pt.SiteTeamId = st.SiteTeamId
            INNER JOIN TeamRole tr ON st.TeamRoleId = tr.RoleId
            INNER JOIN Team t ON tr.TeamId = t.TeamId
              INNER JOIN FundSource fs on fs.FundSourceId = pd.FundSourceId
              INNER JOIN (<* refactor*> SELECT r.BudgetHcmRate
                               FROM Rate r WHERE r.RateName = (SELECT st.RateName
                                                               FROM SiteTeam st INNER JOIN ProjectTeam pt 
                                                                                    ON st.SiteTeamId = pt.SiteTeamId
                                                                                INNER JOIN Baseline bl 
                                                                                    ON bl.ProjectTeamId = pt.ProjectTeamId
                                                               WHERE bl.BaselineId = fblBaselineId
                                                               FETCH FIRST 1 ROW ONLY)
                               ORDER BY r.EffectiveDate
                               FETCH FIRST 1 ROW ONLY) as planHcmRate,
              LEFT OUTER JOIN (SELECT er.Usd_Rate
                                FROM FundEntity fe
                                    INNER JOIN EntityCustomerRate er
                                        ON fe.FundAux1 = er.EntityCustomerRateId) AS hcmRate1 
                                        ON hcmRate1.FundEntityId = b.FundEntityId AND (fs.FundingType IN ('Direct Bill-Single','Direct Bill-Multiple')) AND t.teamType = 'ITDEVMO'
              LEFT OUTER JOIN (<* refactor*> SELECT r.HCMRate
                                 FROM Rate r
                                    INNER JOIN SiteTeam st ON r.RateName = st.RateName
                                    INNER JOIN ProjectTeam pt ON st.SiteTeamId = pt.SiteTeamId
                                 WHERE pt.ProjectTeamId = (SELECT bl.ProjectTeamId
                                                           FROM Baseline bl
                                                           WHERE bl.BaselineId = b.BaselineId)
                                 ORDER BY r.EffectiveDate DESC
                                 FETCH FIRST 1 ROW ONLY) as hcmRate2 ON (fs.FundingType NOT IN ('Direct Bill-Single','Direct Bill-Multiple') OR t.teamType != 'ITDEVMO')
              LEFT OUTER JOIN FundAllocation fa on fa.FundSourceId = pd.FundSourceId AND fa.FundEntityId = b.fblFundEntity
              LEFT OUTER JOIN Request re on bl.ProjectMonth = re.ProjectMonth AND bl.ProjectTeamId = re.ProjectTeamId AND re.BaselineType = 'Reforecast'
              LEFT OUTER JOIN (<*refactor *>SELECT (((SELECT SUM(bl.ForecastAmount) 
                                  FROM Baseline bl 
                                  WHERE DATE(SUBSTR(bl.ProjectMonth, 5) || '-' || 
                                            (CASE SUBSTR(bl.ProjectMonth, 1, 3)
                                                 WHEN 'Jan' THEN '01'
                                                 WHEN 'Feb' THEN '02'
                                                 WHEN 'Mar' THEN '03'
                                                 WHEN 'Apr' THEN '04'
                                                 WHEN 'May' THEN '05'
                                                 WHEN 'Jun' THEN '06'
                                                 WHEN 'Jul' THEN '07'
                                                 WHEN 'Aug' THEN '08'
                                                 WHEN 'Sep' THEN '09'
                                                 WHEN 'Oct' THEN '10'
                                                 WHEN 'Nov' THEN '11'
                                                 WHEN 'Dec' THEN '12'
                                             END) || '-01') > DATE((SELECT CurrentMonth FROM Pmo)) AND
                                        bl.ProjectTeamId = (SELECT ProjectTeamId FROM Baseline WHERE BaselineId = fblBaselineId)) 
                                  +
                                 (SELECT SUM(bl.HcmActualAmount) 
                                  FROM Baseline bl 
                                  WHERE DATE(SUBSTR(bl.ProjectMonth, 5) || '-' || 
                                            (CASE SUBSTR(bl.ProjectMonth, 1, 3)
                                                 WHEN 'Jan' THEN '01'
                                                 WHEN 'Feb' THEN '02'
                                                 WHEN 'Mar' THEN '03'
                                                 WHEN 'Apr' THEN '04'
                                                 WHEN 'May' THEN '05'
                                                 WHEN 'Jun' THEN '06'
                                                 WHEN 'Jul' THEN '07'
                                                 WHEN 'Aug' THEN '08'
                                                 WHEN 'Sep' THEN '09'
                                                 WHEN 'Oct' THEN '10'
                                                 WHEN 'Nov' THEN '11'
                                                 WHEN 'Dec' THEN '12'
                                             END) || '-01') <= DATE((SELECT CurrentMonth FROM Pmo)) AND
                                        bl.ProjectTeamId = (SELECT ProjectTeamId FROM Baseline WHERE BaselineId = fblBaselineId))) as actualForeHcm_pre) AS afh_p
            WHERE ProjectId = projId) as T;
    

    Let me know if this is helpful or you need more explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at this as a baseline explanation of the SQL 2005 Enterprise partitioning.
Most designers use 1024x768 as a baseline for website development. That allows them to
My question is related to this question: Baseline snaplines in custom Winforms controls However,
I'm trying to display the Baseline Start, Finish, Cost, and Work values for a
I am needing an inline image to position 1px below baseline. How can it
A senior developer at my workstation codes like this: table {border-collapse: collapse;border-spacing: 0;} fieldset,img
I ran an old copy of the code to have a baseline to compare
I'm trying to set the width of table column to a minimum value. The
I need to perform a JOIN on a JOIN-ed table, and I'm not sure
Here is my problem, I wants to create a baseline on our development Dateabase

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.