HANA SQL中按订单对相同元素的不同分组

时间:2016-03-27 14:05:19

标签: sql hana

我有一张这样的表:

     NanoTime            Sensor   Key   Rank
15,899,129,832,916     Gyroscope   i     1
15,899,132,632,874     Gyroscope   i     2
15,899,152,377,999     Gyroscope   i     3
15,900,080,214,835     Gyroscope   o     1
15,900,092,388,626     Gyroscope   o     2
15,900,112,529,501     Gyroscope   o     3
15,971,592,577,285     Gyroscope   i     4
15,971,592,739,660     Gyroscope   i     5
15,971,612,339,952     Gyroscope   i     6
15,971,632,305,202     Gyroscope   i     7
15,972,579,736,201     Gyroscope   o     4
15,972,592,583,743     Gyroscope   o     5
15,972,612,371,701     Gyroscope   o     6

我用来创建" Rank"专栏是:

SELECT "NanoTime","Sensor", "Key",
    ROW_NUMBER() OVER ( PARTITION BY "Sensor", "Key" ORDER BY "NanoTime" ASC) as RANK
FROM TEST
WHERE "Sensor" = 'Gyroscope'
GROUP BY "NanoTime","Sensor", "Key"

我想创建一个表,按等级排序"批次"还包括一个" Group"用于分隔每个会话的列(一个会话包含具有相同"键"的所有元素),如下所示。

你可以帮帮我吗?谢谢!

     NanoTime            Sensor   Key   Rank    Group
15,899,129,832,916     Gyroscope   i     1       1
15,899,132,632,874     Gyroscope   i     2       1
15,899,152,377,999     Gyroscope   i     3       1
15,900,080,214,835     Gyroscope   o     1       2
15,900,092,388,626     Gyroscope   o     2       2
15,900,112,529,501     Gyroscope   o     3       2
15,971,592,577,285     Gyroscope   i     1       3
15,971,592,739,660     Gyroscope   i     2       3
15,971,612,339,952     Gyroscope   i     3       3
15,971,632,305,202     Gyroscope   i     4       3
15,972,579,736,201     Gyroscope   o     1       4
15,972,592,583,743     Gyroscope   o     2       4
15,972,612,371,701     Gyroscope   o     3       4

1 个答案:

答案 0 :(得分:0)

这是一个SQLScript代码,可以帮助您根据需要获取分组和排名值

--create table Nano ( NanoTime varchar(30), Sensor varchar(30), key char(1))
/*
insert into Nano values ('15,899,129,832,916','Gyroscope','i');--     1
insert into Nano values ('15,899,132,632,874','Gyroscope','i');--     2
insert into Nano values ('15,899,152,377,999','Gyroscope','i');--     3
insert into Nano values ('15,900,080,214,835','Gyroscope','o');--     1
insert into Nano values ('15,900,092,388,626','Gyroscope','o');--     2
insert into Nano values ('15,900,112,529,501','Gyroscope','o');--     3
insert into Nano values ('15,971,592,577,285','Gyroscope','i');--     4
insert into Nano values ('15,971,592,739,660','Gyroscope','i');--     5
insert into Nano values ('15,971,612,339,952','Gyroscope','i');--     6
insert into Nano values ('15,971,632,305,202','Gyroscope','i');--     7
insert into Nano values ('15,972,579,736,201','Gyroscope','o');--     4
insert into Nano values ('15,972,592,583,743','Gyroscope','o');--     5
insert into Nano values ('15,972,612,371,701','Gyroscope','o');--     6
*/

with cte as (
    select
        *, 
        lag(Sensor,1,'') over (order by NanoTime) previousSensor,
        lag(key,1,'') over (order by NanoTime) previouskey,
        case when 
            lag(Sensor,1,'') over (order by NanoTime) = Sensor and
            lag(key,1,'') over (order by NanoTime) = key
            then 0 else 1 end ischange
    from Nano
    order by NanoTime
)
select NanoTime, Sensor, Key, Row_Number() Over (Partition By GroupNo Order By NanoTime) as Rank, GroupNo
from (
select cte.*, (select sum(x.ischange) from cte x where x.NanoTime <= cte.NanoTime) as groupno
from cte
) t

使用示例数据和上面的SAP HANA SQLScript代码执行,访问以下输出

enter image description here

为了捕获更改组号的数据,我使用了SQL Lag() function,它在数据库平台SAP HANA和SQL Server上非常相似

带有Partition By子句的Row_Number()函数是另一个SQLScript函数(与SQL Server相同),它可以帮助我解决问题。

我希望这就是你想要的