0

我有基于资源 ID 为每个用户分配项目的表格。我们正在创建一些需要通过StaffedDemand价值中扣除价值来获得超额分配价值的报告。

基于 , 的组合,需求值是唯一NumberResourceId

+-----------------------------------------------------------------------------------------------------+
| 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,2),  
  Staffed01 decimal(18,2), 
  Staffed02 decimal(18,2),
  AssociateName varchar(100), 
  GroupName varchar(100)
 )
 
 INSERT INTO table1 VALUES('RR-00000001','1019094', '0.40', '0.40', '0.30', '0.30', 'Raja', 'RR/A')
 INSERT INTO table1 VALUES('RR-00000001','1019094', '0.40', '0.40', '0.70', '0.70', 'Praveen', 'RR/A')
 INSERT INTO table1 VALUES('RR-00000001','1020688', '0.00', '0.00', '0.12', '1.00', 'Bala', 'RR/A')
 INSERT INTO table1 VALUES('RR-00000002','1025136', '0.00', '0.10', '0.00', '0.20', 'Naveen', 'RR/B')
 INSERT INTO table1 VALUES('RR-00000003','1020258', '0.01', '0.01', '0.90', '0.90', 'Kumar', 'RR/C')
 INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.30', '0.30', 'Arun', 'RR/D')
 INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.40', '0.40', 'BBB', 'RR/E')
 INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.30', '0.30', '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

更新

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

4

1 回答 1

2

架构:

CREATE TABLE table1 (
    Number varchar(100),
    ResourceId varchar(100),
    Demand01 decimal(18,2), 
    Demand02 decimal(18,2),  
    Staffed01 decimal(18,2), 
    Staffed02 decimal(18,2),
    AssociateName varchar(100), 
    GroupName varchar(100)
)
 
INSERT INTO table1 VALUES('RR-00000001','1019094', '0.40', '0.40', '0.30', '0.30', 'Raja', 'RR/A')
INSERT INTO table1 VALUES('RR-00000001','1019094', '0.40', '0.40', '0.70', '0.70', 'Praveen', 'RR/A')
INSERT INTO table1 VALUES('RR-00000001','1020688', '0.00', '0.00', '0.12', '1.00', 'Bala', 'RR/A')
INSERT INTO table1 VALUES('RR-00000002','1025136', '0.00', '0.10', '0.00', '0.20', 'Naveen', 'RR/B')
INSERT INTO table1 VALUES('RR-00000003','1020258', '0.01', '0.01', '0.90', '0.90', 'Kumar', 'RR/C')
INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.30', '0.30', 'Arun', 'RR/D')
INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.40', '0.40', 'BBB', 'RR/E')
INSERT INTO table1 VALUES('RR-00000002','1019096', '0.01', '0.01', '0.30', '0.30', '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,resourceid order by rn rows between unbounded preceding and 1 preceding) ,0) PreviousStaffed02    
    FROM
    (       
        SELECT Number, ResourceId, Demand01, Demand02, Staffed01, Staffed02,  AssociateName, GroupName,
            ROW_NUMBER() OVER (PARTITION BY Number, ResourceId ORDER BY (SELECT NULL)) RN
        FROM table1
    ) X
)
select Number, ResourceId, Demand01, Demand02, Staffed01, Staffed02
    , (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
    , AssociateName, GroupName 
from cte

输出:

数字 资源 ID 需求01 需求02 人员配备01 人员配备02 人满为患01 人满为患02 员工姓名 团队名字
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 普拉文 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 纳文 RR/B
RR-00000003 1020258 0.01 0.01 0.90 0.90 -0.89 -0.89 库马尔 RR/C

db<>在这里摆弄

于 2021-03-19T07:10:28.520 回答