如何根据指标列值的变化获取组 row_number()?

How to get group row_number() based on change in indicator column value?

我有 TransactionTime、Amount 和 ResetCountInd (0/1) 列。

目标是获得 运行 总计 Amount 列,从 ResetCountInd=0 行到下一行。

我知道如果我有一个 PartitionNumber 列,从 ResetCountInd=0 开始编号,直到下一个保持不变,我可以使用 window 函数轻松执行 运行 总计。

我不明白如何获取此 PartitionNumber 列。帮忙?

我所追求的可视化。白色我有,黄色我没有 - 特别是 PartitionNumber:

SQL 生成示例数据:

CREATE TABLE mytable(
   TransactionTime datetime NOT NULL PRIMARY KEY
  ,ResetCountInd   BIT  NOT NULL
  ,AddedAmount     INTEGER  NOT NULL
  ,PartitionNumber INTEGER  NOT NULL
  ,RunningTotal    INTEGER  NOT NULL
);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 7:04 AM',0,0,1,0);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 9:14 AM',1,50310,1,50310);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 12:00 PM',1,276229,1,326539);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 1:35 PM',1,45389,1,371928);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 1:52 PM',0,0,2,0);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 3:35 PM',1,108629,2,108629);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 5:04 PM',1,19984,2,128613);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 5:44 PM',1,69338,2,197951);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 6:17 PM',1,126595,2,324546);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/12/16 10:59 PM',1,33720,2,358266);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/13/16 8:57 AM',1,45230,2,403496);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/13/16 11:00 AM',0,0,3,0);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/13/16 11:03 AM',1,155323,3,155323);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/13/16 11:09 AM',0,0,4,0);
INSERT INTO mytable(TransactionTime,ResetCountInd,AddedAmount,PartitionNumber,RunningTotal) VALUES ('1/13/16 12:59 PM',1,67333,4,67333);

组号为指标为零的累加和。您可以在子查询中计算它,然后在外部查询中使用它:

select t.*,
       sum(addedamount) over (partition by grp order by transactiontime) as runningtotal
from (select t.*,
             sum(case when resetcountind = 0 then 1 else 0 end) over (order by transactiontime) as grp
      from mytable t
     ) t

另一个解决方案可能不那么简洁但有效。

SELECT 
    mytable.* 
    ,Partitions.*
    ,RunningTot                     = SUM(mytable.AddedAmount) OVER(PARTITION BY Partitions.PartitionNo ORDER BY mytable.TransactionTime) 

FROM mytable

    CROSS APPLY(SELECT TOP 1
                    PartitionNo                          = COUNT(*) OVER(ORDER BY Partition.ResetCountInd)  --Count admin units within the time window in the WHERE clause

                FROM mytable  AS Partition

                WHERE 
                    Partition.TransactionTime <= mytable.TransactionTime

                ) AS Partitions

这是数据库<>fiddle demo