0

我与 2 张桌子有关系

表 1 -过程

在此处输入图像描述

表 2 -过程历史

在此处输入图像描述

这里的关系是 Id(Process table) 和 ProcessId(Process history table) 我想计算所有进程的平均联网天数。

例如:

nwd = 0;
count = 0;
if(Process.Id = ProcessHistory.ProcessId && ProcessHistory.Status='Status 3') {
  nwd += NWD(Process.CreatedOn, ProcessHistory.CreatedOn);
  count++;
}

预期结果 AverageNWD = nwd/count;

我们怎样才能做到这一点?

4

1 回答 1

2

在脚本中:

使用下面的脚本将向Process表中添加一个新字段 - NetWorkingDays。此字段将包含每个项目的工作日 ( Id)。在数据集中使用此字段将更容易计算 UI 中的平均值(类似于sum(NetWorkingDays) / count(distinct Id)

Process:
Load * Inline [
Id, Name    , CretedOn
1,  Process1, 2019-04-02
2,  Process2, 2019-04-05
3,  Process3, 2019-05-02
4,  Process4, 2019-06-02
];


ProcessHistory:
Load 
  Id        as ProcessHistoryId,
  ProcessId as Id,
  Status,
  CreatedOn as ProcessHistoryCreatedOn
;
Load * Inline [
Id, ProcessId, Status  , CreatedOn
1,  1,         Status 1, 2019-04-02
2,  1,         Status 2, 2019-04-02
3,  1,         Status 3, 2019-04-04
4,  2,         Status 1, 2019-04-05
5,  2,         Status 3, 2019-04-06
6,  3,         Status 1, 2019-05-07
7,  3,         Status 3, 2019-05-09
8,  4,         Status 1, 2019-06-02
9,  4,         Status 2, 2019-06-04
10, 4,         Status 3, 2019-06-07
];


TempTable:
Load
  Id,
  min(CretedOn) as MinCreatedOn
Resident
  Process
Group By
  Id
;

join (TempTable)

Load
  Id,
  max(ProcessHistoryCreatedOn) as MaxCreatedOn
Resident
  ProcessHistory
Where
  Status = 'Status 3'
Group By
  Id
;


NetWorkingDaysData:
Load
  Id,
  NetWorkDays(MinCreatedOn, MaxCreatedOn) as NetWorkingDays 
Resident
  TempTable
;

Drop Table TempTable;

脚本的最后一部分(由内而外):

创建临时表以min(CreatedOn)Process表和max(ProcessHistoryCreatedOn)表中计算ProcessHistoryProcessHistory也被过滤为仅包含其中的记录Status = 'Status 3'(两个表都是按 聚合的Id

TempTable:
Load
  Id,
  min(CretedOn) as MinCreatedOn
Resident
  Process
Group By
  Id
;

join (TempTable)

Load
  Id,
  max(ProcessHistoryCreatedOn) as MaxCreatedOn
Resident
  ProcessHistory
Where
  Status = 'Status 3'
Group By
  Id
;

创建临时表后,我们可以创建最终表,我们将在其中使用NetWorkDays函数计算净工作日数。该NetWorkingDaysData表将只有两个字段 -IdNetWorkingDays

NetWorkingDaysData:
Load
  Id,
  NetWorkDays(MinCreatedOn, MaxCreatedOn) as NetWorkingDays 
Resident
  TempTable
;

最后一步是放弃TempTable- 它不再需要

在用户界面中:

使用下面的表达式可以在 UI 中实现相同的结果。请记住,UI 方法可能会导致更高的资源消耗!由于所有计算都是即时的(取决于您的数据集有多大)

avg(
  Aggr(
    NetWorkDays( min(ProcessHistoryCreatedOn) , max( {< Status = {'Status 3'} >} ProcessHistoryCreatedOn) )
  , Id)
)
于 2019-06-24T14:31:19.207 回答