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

时间:2019-01-13 17:12:38

标签: sql sql-server sql-server-2017

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

目标是获得从“ ResetCountInd = 0”行到下一行的Amount列的总计。

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

我不了解如何获取此PartitionNumber列。帮助吗?

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

enter image description here

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);

2 个答案:

答案 0 :(得分:2)

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

jdbc-mysql

答案 1 :(得分:1)

另一种解决方案可能不那么整洁,但是可以解决。

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

这是db <>小提琴demo