Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate the previous value in T-SQL (update)
    primarykey
    data
    text
    <p>I am struggling a little, not to say, much with some SQL logic.</p> <p><em>Example of the Data set</em></p> <pre><code>wid CCVnr Amount Reference Seq Month ColumIwant=PreviousAmount 11 15946 20 50 1 1/1/2013 NULL 12 15946 20 50 2 1/2/2013 NULL 13 15946 20 50 3 1/3/2013 NULL 14 15946 20 50 4 1/4/2013 NULL 15 15946 20 50 5 1/5/2013 NULL 16 15946 20 50 6 1/6/2013 NULL 35 15946 20 50 1 1/1/2013 NULL 36 15946 10 50 2 1/2/2013 20 37 15946 10 50 3 1/3/2013 20 38 15946 10 50 4 1/4/2013 20 39 15946 10 50 5 1/5/2013 20 40 15946 10 50 6 1/6/2013 20 88 15946 10 50 1 1/1/2013 20 89 15946 20 50 2 1/2/2013 NULL 90 15946 25 50 3 1/3/2013 10 91 15946 25 50 4 1/4/2013 10 92 15946 25 50 5 1/5/2013 10 93 15946 25 50 6 1/6/2013 10 </code></pre> <p>The 5 first columns I have, I want to 'calculate' the last column: PreviousAmount. The problem for me is that every time all 6 months are (re)loaded so it's not enough to look at the 'changed' value, you also have to take into account the history is there to. <br> (I first had a piece of code with a CTE comparing one row to the next but like this I'm missing the history...) </p> <p>Situation here is: a client has an amount he wants to pay per month he can change: In this case the changes it from 20 to 10 to 25.</p> <p>I've uploaded a CSV-file or the very dataset I'm working with here: <a href="https://mega.co.nz/#!oZhC0RxB!CzzMx3Yr6Kx1_1N9scuNqwJnqoZDGUXte47iOPkLG-E" rel="nofollow">https://mega.co.nz/#!oZhC0RxB!CzzMx3Yr6Kx1_1N9scuNqwJnqoZDGUXte47iOPkLG-E</a></p> <p>We using SQL Server 2008 R2. There are 20M rows like this in the table. So maybe a cursor is not the best option(?)</p> <p>Thanks a lot in advance for any help !! L</p> <p><br>UPDATE: I added a wid column because the original data set also holds a wid column. @<strong>Mark Bannister</strong> The logic is to look at 'Amount' and when for a certain month the Amount is changed by the user: * All the next months get updated when the 'New Amount' * We calculate the Previous Amount which is = the Amount is was before</p> <p>@<strong>Love2Learn</strong> also thanks for your comment. There are lots more columns but you have to know it's not easy to add more columns in the stackoverflow code block. Therefore I also uploaded a CSV file from which you can find the link. A relevant column I do have added is the 'wid'.</p> <p>Normally there should be enough info and columns to calculate this previous row. Thanks in advance! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload