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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:40:45+00:00 2026-05-30T19:40:45+00:00

I have a Table with these columns – ProjectId – Generation – Expected –

  • 0

I have a Table with these columns

- ProjectId
 - Generation
 - Expected 
 - CarryOver

I am trying to update my already populated table in this fashion:

  Generation = Integral Part of ((Generation + CarryOver of Previous Row)/10 )
  CarryOver =  decimal part of  ((Generation + CarryOver of Previous Row)/10 )

where Previous Row and Current Row both have same projectId

Below is the query am using to achieve this:

UPDATE TTable 

SET 

TTable.Expected=(TTable.Generation+ ISNULL(STable.CarryOver,0)),
TTable.CarryOver =(TTable.Generation+ISNULL(STable.CarryOver,0))-CONVERT(INT,(TTable.Generation+ISNULL(STable.CarryOver,0)))

FROM
(
    SELECT ROW_NUMBER()OVER(order by ProjectId,MonthYear) as RowNumber,ProjectId, 
    [MonthYear],[Month],[Generation],[Expected],[CarryOver]
    FROM #SRECEsimated 
)TTable,

(
    SELECT ROW_NUMBER()OVER(order by ProjectId,MonthYear) as RowNumber,ProjectId,
        [MonthYear],[Month],[Generation],[Expected],[CarryOver] 
    FROM #SRECEsimated 
) STable


Where   
TTable.RowNumber  = STable.RowNumber+1 AND
TTable.ProjectId = STable.ProjectId 

….but something strange happens, update happens only for first two rows. For other rows,
ISNULL(STable.CarryOver,0) returns 0. why??
Please help me. or suggest some other way to achieve this

EDIT: on running the query

ProjectId   MonthYear   Month   Year    Generation  Expected    CarryOver
10  2011-10-01 00:00:00.000 10  2011    56.748             56            0.748
10  2011-11-01 00:00:00.000 11  2011    12.004             12            0.752
10  2011-12-01 00:00:00.000 12  2011    10.632             10            0.632
10  2012-01-01 00:00:00.000 01  2012    11.928             11            0.928
10  2012-02-01 00:00:00.000 02  2012    7.580               7            0.580
100 2011-12-01 00:00:00.000 12  2011    5.897               5            0.897
100 2012-01-01 00:00:00.000 01  2012    0.881               1            0.778

data is generated as shown above. notice how the logic doesn’t work after 3row

Original Ouput. Before running the update query:

ProjectId   MonthYear   Month   Year    Generation  Expected    CarryOver
10  2011-10-01 00:00:00.000 10  2011    56.748            56             0.748
10  2011-11-01 00:00:00.000 11  2011    12.004            NULL           NULL
10  2011-12-01 00:00:00.000 12  2011    10.632            NULL           NULL
10  2012-01-01 00:00:00.000 01  2012    11.928            NULL           NULL
10  2012-02-01 00:00:00.000 02  2012    7.580             NULL           NULL
100 2011-12-01 00:00:00.000 12  2011    5.897             5           0.897
100 2012-01-01 00:00:00.000 01  2012    0.881             NULL           NULL
  • 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-30T19:40:46+00:00Added an answer on May 30, 2026 at 7:40 pm

    The following query will give you the expected results

    (Test code https://gist.github.com/1969171 for those that want to play)

    PID Date       M  Y    Generation  Expected CarryOver   
    10  2011-10-01 10 2011 56.748      56       0.748
    10  2011-11-01 11 2011 12.004      12       0.752
    10  2011-12-01 12 2011 10.632      11       0.384
    10  2012-01-01 01 2012 11.928      12       0.312
    10  2012-02-01 02 2012 7.58        7        0.892
    100 2011-12-01 12 2011 5.897       5        0.897
    100 2012-01-01 01 2012 0.881       1        0.778
    

    DECLARE rowItems CURSOR FOR 
    SELECT ProjectId, [Month], [Year], Generation FROM TTable
    ORDER BY ProjectId,[Year] ,CAST([Month] as int)
    
    DECLARE @p int, @m VARCHAR(5), @y int, @g FLOAT, @priorP int, 
            @carryOver FLOAT, @expected FLOAT
    
    OPEN rowItems
    
    FETCH NEXT FROM rowItems INTO @p, @m, @y, @g
    
    SET @priorP = -1
    SET @carryOver = 0.0
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
    
       IF NOT @p = @priorP SET @carryOver = 0.0
    
       SET @expected = @g+@carryOver
       SET @carryOver = ROUND(@expected-FLOOR(@expected),3,0)   
    
       UPDATE TTable
         SET EXPECTED = FLOOR(@expected), CarryOver = @carryOver
       WHERE  ProjectId = @p and [Month] = @m and  [Year] = @y
    
       SET @priorP = @p
    
       FETCH NEXT FROM rowItems INTO @p, @m, @y, @g
    
    END
    
    CLOSE rowItems
    DEALLOCATE rowItems
    
    SELECT * FROM TTable
    

    You need to do this in a loop. The query you use is getting the original value of the carry over column not the updated value.

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

Sidebar

Related Questions

Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY
I have a table with these columns: win, los, id ... I want to
For examples sake, lets say I have a table containing these columns ID (primary
I have a table in an Openoffice Database, containing two date columns. I'm trying
In oracle: I have a table rel with these columns (object_id1, object_id2) that relates
Say you have table of some items with these two columns: ItemName Price ....where
I have an exist model (and table) with these columns: id , name ,
I have a table with these columns create table demo ( ID integer not
Say I have a table Comments with these columns: Id, Comment, Category, CreatedDate, CommenterId
I have a table in my DB called Emplyee and contains these columns :

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.