I have a table
Id | Aisle | OddEven | Bay | Size | Y-Axis 3 | A1 | Even | 14 | 10 | 100 1 | A1 | Even | 16 | 10 | 6 | A1 | Even | 20 | 10 | 12 | A1 | Even | 26 | 5 | 150 10 | A1 | Even | 28 | 5 | 11 | A1 | Even | 32 | 5 | 2 | A1 | Odd | 13 | 10 | 100 5 | A1 | Odd | 17 | 10 | 4 | A1 | Odd | 19 | 10 | 9 | A1 | Odd | 23 | 5 | 150 7 | A1 | Odd | 25 | 5 | 8 | A1 | Odd | 29 | 5 |
want to look like this
Id | Aisle | OddEven | Bay | Size | Y-Axis 1 | A1 | Even | 14 | 10 | 100 2 | A1 | Even | 16 | 10 | 110 3 | A1 | Even | 20 | 10 | 120 4 | A1 | Even | 26 | 5 | 150 5 | A1 | Even | 28 | 5 | 155 6 | A1 | Even | 32 | 5 | 160 7 | A1 | Odd | 13 | 10 | 100 8 | A1 | Odd | 17 | 10 | 110 9 | A1 | Odd | 19 | 10 | 120 10 | A1 | Odd | 23 | 5 | 150 11 | A1 | Odd | 25 | 5 | 155 12 | A1 | Odd | 29 | 5 | 160
I need a select query and update query. What its doing is there are already some Y-Axis Number been filled (at the start of the Odd/Even) then I need to take the previous row's Y-Axis column's value and adds to the current rows's size which = to current Y-Axis. Needs to keep doing it until it finds another Y-Axis has the value it skips the calculation and next row is using that number.
My thinking process is this:
Id will definitely be used, however, the Id is not sequence as shown my example
so I need to have
ROW_Number OVER (PARTITION BY Aisle,OddEven,Bay Order BY Aisle,OddEven,Bay)
Then some kind of JOIN the same table but the ON is T1.RN = T2.RN - 1
Where I am stuck is but the first row has not previous value it will try to update that value.
Anyone have an idea for SQL Query 2008 for Select and Update will be greatly appreciated! Thanks.
You seem to want a cumulative sum. This would be easier in SQL Server 2012+. You can do this in SQL Server 2008 using
outer apply
:I gotta leave my computer so update query should be easy to move on from here.
Below is the select query;
A little more difficult on 2008, but I think this is what you are looking for
Returns