我有下面的简单表格,大约有。 40k 项目:
|date|consum|
基于这个表,我想计算一些新的列。最后,表格应该是这样的:
|date|consum|row|row_relativ|min|max|upper|lower|
很遗憾,SAP HANA 数据库不支持递归表语句...
计算规则如下:
row_number() over(order by dates)
row*35040/32768
floor(row_relative)
ceil(row_relative)
目前我有一个包含 5 个变量的存储过程。在每个变量中,我计算 1 个新列。它非常非常脏……我怎样才能让它与众不同?我不能使用递归函数...:/
这是我的 SQL 片段...玩得开心 =)
var1 = select *, row_number() over(order by dates) as row from XYZ order by dates limit 34944;
var2 = select *, (row * 34944/32768) as row_relative from :var1;
var3 = select *, floor(row_relative) as min, ceil(row_relative) as max from :var2;
var4 = select
p.*,
(select consumfrom :var3 where row = p.min) as lower,
(select consumfrom :var3 where row = p.max) as upper
from :var3 p;
var5 = select
p.*,
(p.lower* (1-p.row_relative+p.min)+p.upper * (p.row_relativ - p.min)) as new_consum
from :var4 p;
您可以在 SQLScript 中使用以下 CTE 公用表表达式 顺便说一句,非常有趣的是,几乎相同的 SQL Server CTE 语法与 SAP HANA SQLScript 语法匹配:)
with cte as (
select
date,
consum,
row_number() over (order by date) as row
from rawdata
)
select
date,
consum,
row,
row * 35040 / 32768 as row_relative,
floor( (row * 35040 / 32768) ) as min,
ceil( (row * 35040 / 32768) ) as max,
(select cte2.consum from cte cte2 where row = floor( (cte.row * 35040 / 32768) ) ) as min2,
(select cte2.consum from cte cte2 where row = ceil( (cte.row * 35040 / 32768) ) ) as max2
from cte