如何使用上一行的列的值来计算下一行的列的值

时间:2016-07-29 15:59:48

标签: sql sql-server sql-server-2008

我有一张桌子

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    |

希望看起来像这样

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

我需要一个选择查询和更新查询。它的作用是已经填充了一些Y轴编号(在奇数/偶数的开始处)然后我需要取前一行的Y轴列的值并添加到当前行的大小=当前Y-轴。需要继续这样做,直到找到另一个Y轴,它具有跳过计算的值,下一行使用该数字。

我的思考过程是这样的:

肯定会使用Id,但是Id不是我的示例所示的序列

所以我需要

ROW_Number OVER(Aisle,OddEven,Bay Order BY Aisle,OddEven,Bay的分区)

然后某种JOIN在同一张表中但是ON是T1.RN = T2.RN - 1

我被困的地方但是第一行没有以前的值,它会尝试更新该值。

任何人都对SQL Query 2008 for Select和Update有所了解将不胜感激!感谢。

3 个答案:

答案 0 :(得分:2)

您似乎想要累积总和。这在SQL Server 2012+中会更容易。您可以使用outer apply

在SQL Server 2008中执行此操作
select t.*, cume_value
from t outer apply
     (select sum(size) + sum(yaxis) as cume_value
      from t t2
      where t2.aisle = t.aisle and t2.oddeven = t.oddeven and
            t2.bay < t.bay
     ) t2;

答案 1 :(得分:1)

2008年有点困难,但我认为这就是你要找的东西

$( "#arrow-left" ).on( "touchstart", function(){
    var senddata = { "room": roomId, "action": "d-l" };
    socket.emit( "player-move", senddata );
});

返回

Declare @Table table (Id int,Aisle varchar(25),OddEven varchar(25),Bay int,Size int,[Y-Axis] int)
Insert Into @Table values
(3,'A1','Even',14,10 ,100),
(1,'A1','Even',16,10 ,0),
(6,'A1','Even',20,10 ,0),
(12,'A1','Even',26,5,150),
(10,'A1','Even',28,5,0),
(11,'A1','Even',32,5,0),
(2,'A1','Odd',13,10 ,100),
(5,'A1','Odd',17,10 ,0),
(4,'A1','Odd',19,10 ,0),
(9,'A1','Odd',23,5,150),
(7,'A1','Odd',25,5,0),
(8,'A1','Odd',29,5,0)

;with cteBase as (
      Select *
            ,IDNew=Row_Number() over (Order By Aisle,Bay) 
            ,RowNr=Row_Number() over (Order By Aisle,OddEven,Bay) 
       From  @Table
 )
 , cteGroup as (Select TmpRowNr=RowNr,GrpNr=Row_Number() over (Order By RowNr) from cteBase where [Y-Axis]>0)
 , cteFinal as (
                Select A.*
                      ,GrpNr = (Select max(GrpNr) from cteGroup Where TmpRowNr<=RowNr)
                 From  cteBase A
                )
Select ID=Row_Number() over (Order By A.OddEven,A.Bay)
      ,A.Aisle
      ,A.OddEven
      ,A.Bay
      ,A.Size
      ,[Y-Axis] = Sum(case when B.[Y-Axis]>0 then B.[Y-Axis] else B.Size end) 
 From  cteFinal A
 Join  cteFinal B on (B.RowNr<=A.RowNr and A.GrpNr=B.GrpNr)
 Group By
       A.IDNew
      ,A.Aisle
      ,A.OddEven
      ,A.Bay
      ,A.Size 
 Order By A.OddEven,A.Bay

答案 2 :(得分:0)

我必须离开我的电脑,因此更新查询应该很容易从这里开始。

以下是选择查询;

select  row_number() over (order by oddeven,bay) id,
    Aisle,
    OddEven,
    Bay,
    Size,
    max(ISNULL([Y-Axis],0)) over (partition by Aisle, OddEven,Size order by bay) 
        + sum(CASE WHEN [Y-Axis] is null THEN Size ELSE 0 END) over (partition by Aisle,OddEven,size order by Bay) as [Y-Axis]
from oddseven
order by id