简体   繁体   English

SAP Hana-SQL-计算同一表上的列而无需递归函数

[英]SAP Hana - SQL - calculate columns on the same table without recursive functions

I have the following simple table with approx. 我有下面的简单表。 40k items: 40k件:

|date|consum|

Based on this table, I want to calculate some new columns. 基于此表,我想计算一些新列。 At the end, the table should looks like: 最后,该表应如下所示:

|date|consum|row|row_relativ|min|max|upper|lower|

Unfortunately the SAP Hana Database does not support recursive table statements... 不幸的是,SAP Hana数据库不支持递归表语句。

Here are the calculation rules: 计算规则如下:

  1. row = row_number() over(order by dates) row = row_number() over(order by dates)
  2. row_relative = row*35040/32768 row_relative = row*35040/32768 35040/32768
  3. min = floor(row_relative) 最小值= floor(row_relative)
  4. max = ceil(row_relative) 最大值= ceil(row_relative)
  5. min = select the consum from the row with the min value min =从具有最小值的行中选择消费
  6. max = select the consum from the row with the max value max =从具有最大值的行中选择消费

At the moment I have a stored precedure with 5 variables. 目前,我有5个变量的存储过程。 In each variable I calculate i new coulmn. 在每个变量中,我都会计算出新的coulmn。 It is very very dirty... How can i make it different? 它非常非常脏...我该如何改变它? I cant use recursive functions... :/ 我不能使用递归函数...:/

Here is my sql snippet... have fun =) 这是我的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;

You can use following CTE Common Table Expression in your SQLScript BTW, it is very interesting that nearly the same SQL Server CTE syntax matches the SAP HANA SQLScript syntax :) 您可以在SQLScript BTW中使用以下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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM