我有一张下面结构的桌子
ID - Task | Performed BY | Start Date | End Date
1 --- Task 3 --- John --------- 9/16/2017 - 9/17/2017
1 --- Task 2 --- Jim ----------- 9/14/2017 - 9/16/2017
1 --- Task 1 --- John --------- 9/10/2107 - 9/13/2017
2 --- Task 2 --- Jane --------- 9/14/2017 - 9/15/2017
2 --- Task 1 --- Claire -------- 9/12/2017 - 9/13/2017
我需要创建一个新列,显示为SAP HANA中的每个ID执行所有任务所花费的总时间(持续时间)。即最后一个任务的结束日期减去每个ID的第一个任务的开始日期。
以下是我需要的结果:
ID - Task | Performed BY | Start Date | End Date | Duration
1 --- Task 3 --- John --------- 9/16/2017 - 9/17/2017 -- 7 Days
1 --- Task 2 --- Jim ----------- 9/14/2017 - 9/16/2017 -- 7 Days
1 --- Task 1 --- John --------- 9/10/2107 - 9/13/2017 -- 7 Days
2 --- Task 2 --- Jane --------- 9/14/2017 - 9/15/2017 -- 3 Days
2 --- Task 1 --- Claire -------- 9/12/2017 - 9/13/2017 -- 3 Days
这可以使用以下查询在SQL中完成:
SELECT ID,任务,执行者,开始日期,结束日期, DATEDIFF(日, MIN(开始日期)OVER(PARTITION BY ID), MAX(结束日期)OVER(PARTITION BY ID))持续时间 FROM Table;
我需要使用SAP HANA来执行此任务。我非常感谢有关如何将此SQL查询迁移到HANA的任何帮助。
谢谢!
SAP HANA支持标准SQL window functions,还提供日期计算功能days_between
。
您可以像这样编写代码:
SELECT "ID", "Task", "Performed By", "Start Date", "End Date"
, DAYS_BETWEEN(MIN("Start Date") OVER (PARTITION BY "ID"),
MAX("End Date") OVER (PARTITION BY "ID")) "Duration"
FROM tasks
order by "ID", "Task";
ID Task Performed By Start Date End Date Duration
1 Task 1 John 10/09/2017 13/09/2017 7
1 Task 2 Jim 14/09/2017 16/09/2017 7
1 Task 3 John 16/09/2017 17/09/2017 7
2 Task 1 Claire 12/09/2017 13/09/2017 3
2 Task 2 Jane 14/09/2017 15/09/2017 3