根据前一行结果动态计算行值

问题描述

我有根据资源 ID 为每个用户分配项目的表格。我们正在创建一些需要通过从 Staffed 值中减去 Demand 值来获得超额分配值的仓库。

需求值是唯一的,基于 Number,ResourceId 的组合。

+-----------------------------------------------------------------------------------------------------+
| Number      | ResourceId | Demand01 | Demand02 |  Staffed01 | Staffed02 | AssociateName | GroupName |
+-----------------------------------------------------------------------------------------------------+
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.30      | 0.30      | Raja         |  RR/A      |
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.70      | 0.70      | Praveen      |  RR/A      |
| RR-00000001 | 1020688    | 0.00     |  0.00    |  0.12      | 1.00      | Bala         |  RR/A      |
| RR-00000002 | 1025136    | 0.00     |  0.00    |  0.00      | 0.00      | Naveen       |  RR/B      |
| RR-00000003 | 1020258    | 0.01     |  0.01    |  0.90      | 0.90      | Kumar        |  RR/C      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      | Arun         |  RR/D      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.70      | 0.70      | BBB          |  RR/E      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      | CCC          |  RR/E      |
+-----------------------------------------------------------------------------------------------------+

预期输出

+-----------------------------------------------------------------------------------------------------------------------------------------+
| Number      | ResourceId | Demand01 | Demand02 |  Staffed01 | Staffed02 |  OverStaffed01 | OverStaffed02 |  AssociateName  |  GroupName |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.30      | 0.30      |     0.00       |     0.00      |    Raja         |  RR/A      |
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.70      | 0.70      |    -0.60       |    -0.60      |    Praveen      |  RR/A      |
| RR-00000001 | 1020688    | 0.00     |  0.00    |  0.12      | 1.00      |    -0.12       |    -1.00      |    Bala         |  RR/A      |
| RR-00000002 | 1025136    | 0.00     |  0.10    |  0.00      | 0.20      |     0.00       |    -0.20      |    Naveen       |  RR/B      |
| RR-00000003 | 1020258    | 0.01     |  0.01    |  0.90      | 0.90      |    -0.89       |    -0.89      |    Kumar        |  RR/C      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      |    -0.29       |    -0.29      |    Arun         |  RR/D      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.40      | 0.40      |    -0.40       |    -0.40      |    BBB          |  RR/E      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      |    -0.30       |    -0.30      |    CCC          |  RR/E      |
+-----------------------------------------------------------------------------------------------------------------------------------------+

我必须推导出 OverStaffed01 = Demand01 - Staffed01 但有一次约束,如果我们有重复 NumberResourceId,我们必须根据前一行调整需求值。

例如:

Number:RR-00000002 and ResourceId:1019096 的组合有三行,所以我们不能直接计算值。

对于第一行,(OverStaffed01 = Demand01 - Staffed01) 0.40 - 0.30 是 大于 0 表示人员不足,我们认为是 0。

对于第二行 (OverStaffed01 = Demand01 - Staffed01) 0.10 - 0.70 (因为我们已经从第一行减少了 0.30,所以我们有 Demand01 为 0.10),而 OverStaffed01 为 -0.60

Number:RR-00000001 and ResourceId:1019094 的组合有两行,所以我们不能直接计算值。

对于第一行,(OverStaffed01 = Demand01 - Staffed01) 0.01 - 0.30 是 -0.29。

对于第二行 (OverStaffed01 = Demand01 - Staffed01) 0.00 - 0.40 (因为我们已经从第一行减少了 0.01,所以我们有 Demand01 作为 0) 和 OverStaffed01 是 -0.40

对于第三行 (OverStaffed01 = Demand01 - Staffed01) 0.00 - 0.30 (因为我们已经从第一行减少了 0.01,所以我们有 Demand01 为 0),而 OverStaffed01 为 -0.30

对于其他行,我们有唯一的Number和ResourceId,所以我们可以直接推算OverStaffed01

到目前为止尝试过的查询

CREATE TABLE table1
(
  Number varchar(100),ResourceId varchar(100),Demand01 decimal(18,2),Demand02 decimal(18,Staffed01 decimal(18,Staffed02 decimal(18,AssociateName varchar(100),GroupName varchar(100)
 )
 
 INSERT INTO table1 VALUES('RR-00000001','1019094','0.40','0.30','Raja','RR/A')
 INSERT INTO table1 VALUES('RR-00000001','0.70','Praveen','1020688','0.00','0.12','1.00','Bala','RR/A')
 INSERT INTO table1 VALUES('RR-00000002','1025136','0.10','0.20','Naveen','RR/B')
 INSERT INTO table1 VALUES('RR-00000003','1020258','0.01','0.90','Kumar','RR/C')
 INSERT INTO table1 VALUES('RR-00000002','1019096','Arun','RR/D')
 INSERT INTO table1 VALUES('RR-00000002','BBB','RR/E')
 INSERT INTO table1 VALUES('RR-00000002','CCC','RR/E')


 SELECT Number,ResourceId,Demand01,Staffed01,AssociateName,GroupName,ROW_NUMBER() OVER (PARTITION BY Number,ResourceId ORDER BY (Demand01 - Staffed01) DESC,(Demand02 - Staffed02) DESC) RN,Demand01- SUM (Staffed01) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS OverStaffed01,Demand02- SUM (Staffed02) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS OverStaffed02
FROM table1

更新

我已经发布了我最近更新的查询,它以某种方式给出了正确的结果,但仍然不是预期的结果。

解决方法

架构:

CREATE TABLE table1 (
    Number varchar(100),ResourceId varchar(100),Demand01 decimal(18,2),Demand02 decimal(18,Staffed01 decimal(18,Staffed02 decimal(18,AssociateName varchar(100),GroupName varchar(100)
)
 
INSERT INTO table1 VALUES('RR-00000001','1019094','0.40','0.30','Raja','RR/A')
INSERT INTO table1 VALUES('RR-00000001','0.70','Praveen','1020688','0.00','0.12','1.00','Bala','RR/A')
INSERT INTO table1 VALUES('RR-00000002','1025136','0.10','0.20','Naveen','RR/B')
INSERT INTO table1 VALUES('RR-00000003','1020258','0.01','0.90','Kumar','RR/C')
INSERT INTO table1 VALUES('RR-00000002','1019096','Arun','RR/D')
INSERT INTO table1 VALUES('RR-00000002','BBB','RR/E')
INSERT INTO table1 VALUES('RR-00000002','CCC','RR/E')

查询:

with cte as (
    SELECT Number,ResourceId,Demand01,Demand02,Staffed01,Staffed02,AssociateName,GroupName,RN,coalesce(sum(staffed01)over (partition by number,resourceid order by rn rows between unbounded preceding and 1 preceding),0) PreviousStaffed01,coalesce(sum(staffed02)over (partition by number,0) PreviousStaffed02    
    FROM
    (       
        SELECT Number,ROW_NUMBER() OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL)) RN
        FROM table1
    ) X
)
select Number,(case when ((case when (demand01-previousstaffed01) <0 then 0 else (demand01-previousstaffed01)end) -staffed01)<0
    then ((case when (demand01-previousstaffed01) <0 then 0 else (demand01-previousstaffed01)end) -staffed01) else 0 end) OverStaffed01,(case when ((case when (demand02-previousstaffed02) <0 then 0 else (demand02-previousstaffed02)end) -staffed02)<0
    then ((case when (demand02-previousstaffed02) <0 then 0 else (demand02-previousstaffed02)end) -staffed02) else 0 end) OverStaffed02,GroupName 
from cte

输出:

数字 ResourceId Demand01 Demand02 Staffed01 Staffed02 OverStaffed01 OverStaffed02 AssociateName GroupName
RR-00000001 1019094 0.40 0.40 0.30 0.30 0.00 0.00 王爷 RR/A
RR-00000001 1019094 0.40 0.40 0.70 0.70 -0.60 -0.60 Praveen RR/A
RR-00000001 1020688 0.00 0.00 0.12 1.00 -0.12 -1.00 巴拉 RR/A
RR-00000002 1019096 0.01 0.01 0.30 0.30 -0.29 -0.29 阿伦 RR/D
RR-00000002 1019096 0.01 0.01 0.40 0.40 -0.40 -0.40 BBB RR/E
RR-00000002 1019096 0.01 0.01 0.30 0.30 -0.30 -0.30 CCC RR/E
RR-00000002 1025136 0.00 0.10 0.00 0.20 0.00 -0.10 Naveen RR/B
RR-00000003 1020258 0.01 0.01 0.90 0.90 -0.89 -0.89 库马尔 RR/C

dbfiddle here

,

感谢您的回答Kazi Mohammad Ali Nur

我尝试编写查询 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,但查询有点复杂。

SELECT Number,CASE WHEN RN = 1 THEN OverStaffed01 
      ELSE CASE WHEN  SIGN(LAG(OverStaffed01,1,0) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL))) = 0 THEN OverStaffed01 
            ELSE 0 - Staffed01
        END 
      END OverStaffed01,CASE WHEN RN = 1 THEN OverStaffed02
      ELSE CASE WHEN  SIGN(LAG(OverStaffed02,ResourceId ORDER BY (SELECT NULL))) = 0 THEN OverStaffed02 
            ELSE 0 - Staffed02
        END
      END OverStaffed02
FROM
(
    SELECT Number,ResourceId ORDER BY (Demand01 - Staffed01) DESC,(Demand02 - Staffed02) DESC) RN,CASE WHEN Demand01 - SUM (Staffed01) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) > 0 THEN 0 
                ELSE Demand01 - SUM (Staffed01) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 
            END AS OverStaffed01,CASE WHEN Demand02 - SUM (Staffed02) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) > 0 THEN 0 
                ELSE Demand02 - SUM (Staffed02) OVER (PARTITION BY Number,ResourceId ORDER BY (SELECT NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 
        END AS OverStaffed02                
    FROM table1
)Tmp

SQL FIDDLE

+-----------------------------------------------------------------------------------------------------------------------------------------+
| Number      | ResourceId | Demand01 | Demand02 |  Staffed01 | Staffed02 |  OverStaffed01 | OverStaffed02 |  AssociateName  |  GroupName |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.30      | 0.30      |     0.00       |     0.00      |    Raja         |  RR/A      |
| RR-00000001 | 1019094    | 0.40     |  0.40    |  0.70      | 0.70      |    -0.60       |    -0.60      |    Praveen      |  RR/A      |
| RR-00000001 | 1020688    | 0.00     |  0.00    |  0.12      | 1.00      |    -0.12       |    -1.00      |    Bala         |  RR/A      |
| RR-00000002 | 1025136    | 0.00     |  0.10    |  0.00      | 0.20      |     0.00       |    -0.20      |    Naveen       |  RR/B      |
| RR-00000003 | 1020258    | 0.01     |  0.01    |  0.90      | 0.90      |    -0.89       |    -0.89      |    Kumar        |  RR/C      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      |    -0.29       |    -0.29      |    Arun         |  RR/D      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.40      | 0.40      |    -0.40       |    -0.40      |    BBB          |  RR/E      |
| RR-00000002 | 1019096    | 0.01     |  0.01    |  0.30      | 0.30      |    -0.30       |    -0.30      |    CCC          |  RR/E      |
+-----------------------------------------------------------------------------------------------------------------------------------------+