在下个月添加上个月的数据,SAP HANA 计算视图中的数据历史化
Add Data of previous months in next month,Historization of data in SAP HANA Calculation View
历史化意味着我们将前几个月的数据添加到即将到来的月份。
例如:输入:
ID STATUS YEAR MONTH
A OPEN 2017-01
A CLOSED 2017-03
B OPEN 2017-01
B Closed 2017-05
我的O/P:
YEAR MONTH COUNT-OPEN COUNT-CLOSED
2017-01 2(both A & B OPEN) 0
2017-02 2(FROM 201701) 0
2017-03 2(from 2017-02)-1(A Closed now)=1 1
2017-04 1(from 201703)=1 1(from prev month)
2017-05 1-1(b closed)=0 1+1(b closed)=2
值是实际值 o/p 我刚刚写了公式是为了让你理解其中的逻辑。
我需要将数据添加到从上个月到下个月的打开/关闭,是否可以在 SAP HANA W/O 中使用 Cursors?
我可以用 Cursors 做到这一点,如果存在任何其他逻辑,请帮助我!
对于这个需求的解决方案,我可以建议你在 HANA 数据库上使用数字 table。在接下来的部分中,我分享了 SQLScript 代码,其中我使用 numbers table function 创建特定范围内的一系列日期
with input as (
select
id,
status,
year,
cast(month as integer) month,
case when status = 'OPEN' then 1 else -1 end as COUNT_OPEN,
case when status = 'CLOSED' then 1 else 0 end as COUNT_CLOSED
from Historization
)
SELECT
y.rownum as year,
m.rownum as month,
(select sum(COUNT_OPEN) from input where year <= y.rownum and month <= m.rownum) as COUNT_OPEN,
(select sum(COUNT_CLOSED) from input where year <= y.rownum and month <= m.rownum) as COUNT_CLOSED
FROM Numbers_Table(2017) as y
CROSS JOIN Numbers_Table(12) as m
where y.rownum = 2017
order by y.rownum, m.rownum
你看,我在外部 SELECT 语句中使用了数字函数 "Numbers_Table",它创建了连接子句的 FROM 部分
为了模拟 COUNT,我假设一条记录在打开之前不能关闭,所以当它处于打开状态时,我将 OPEN 状态计数加 1。
当记录关闭时,我将 1 加到 CLOSED 状态。但这里的诀窍是,我将 -1 用于关闭项目的 OPEN 状态。
SUM() 聚合函数帮我得到最后一行数据
输出结果如下,
历史化意味着我们将前几个月的数据添加到即将到来的月份。
例如:输入:
ID STATUS YEAR MONTH
A OPEN 2017-01
A CLOSED 2017-03
B OPEN 2017-01
B Closed 2017-05
我的O/P:
YEAR MONTH COUNT-OPEN COUNT-CLOSED
2017-01 2(both A & B OPEN) 0
2017-02 2(FROM 201701) 0
2017-03 2(from 2017-02)-1(A Closed now)=1 1
2017-04 1(from 201703)=1 1(from prev month)
2017-05 1-1(b closed)=0 1+1(b closed)=2
值是实际值 o/p 我刚刚写了公式是为了让你理解其中的逻辑。 我需要将数据添加到从上个月到下个月的打开/关闭,是否可以在 SAP HANA W/O 中使用 Cursors? 我可以用 Cursors 做到这一点,如果存在任何其他逻辑,请帮助我!
对于这个需求的解决方案,我可以建议你在 HANA 数据库上使用数字 table。在接下来的部分中,我分享了 SQLScript 代码,其中我使用 numbers table function 创建特定范围内的一系列日期
with input as (
select
id,
status,
year,
cast(month as integer) month,
case when status = 'OPEN' then 1 else -1 end as COUNT_OPEN,
case when status = 'CLOSED' then 1 else 0 end as COUNT_CLOSED
from Historization
)
SELECT
y.rownum as year,
m.rownum as month,
(select sum(COUNT_OPEN) from input where year <= y.rownum and month <= m.rownum) as COUNT_OPEN,
(select sum(COUNT_CLOSED) from input where year <= y.rownum and month <= m.rownum) as COUNT_CLOSED
FROM Numbers_Table(2017) as y
CROSS JOIN Numbers_Table(12) as m
where y.rownum = 2017
order by y.rownum, m.rownum
你看,我在外部 SELECT 语句中使用了数字函数 "Numbers_Table",它创建了连接子句的 FROM 部分
为了模拟 COUNT,我假设一条记录在打开之前不能关闭,所以当它处于打开状态时,我将 OPEN 状态计数加 1。 当记录关闭时,我将 1 加到 CLOSED 状态。但这里的诀窍是,我将 -1 用于关闭项目的 OPEN 状态。 SUM() 聚合函数帮我得到最后一行数据
输出结果如下,