Thanks for the DDL and INSERT statements.
I really liked this question and I spend some time tinkering with it.
Unfortunately I did not find an elegant solution for this, since SAP HANA currently doesn't support recursive queries.
Because the new starting point for any 24 hour interval is based on a calculated value in a row "before" the current one and there could be any number of "before" rows, neither join nor sub-select work here.
The solution to this, unfortunately seems to be a procedure:
create global temporary column table my_tmp (record_number bigint, create_date seconddate); drop procedure max_by_24_hrs; create procedure max_by_24_hrs (out max_events TABLE (record_number bigint, create_date seconddate)) language SQLSCRIPT as begin declare cursor c_raw for select record_number, create_date from test_table order by record_number, create_date; declare curr_grp_record bigint ; declare curr_grp_date seconddate ; declare curr_record bigint; declare curr_date seconddate; -- initialize the loop variables truncate table my_tmp; select min (record_number), min (create_date) into curr_grp_record, curr_grp_date from test_table; for cr as c_raw DO curr_record := cr.record_number; curr_date := cr.create_date; -- records group if :curr_record > :curr_grp_record then -- new group, new date curr_grp_record = :curr_record; curr_grp_date = :curr_date; insert into my_tmp values (:curr_record, :curr_grp_date); elseif (:curr_record = :curr_grp_record and add_days(:curr_grp_date, 1) <= :curr_date) then -- same record no, but date is at least a day higher than the last starting point -- new date curr_grp_date = :curr_date; insert into my_tmp values (:curr_record, :curr_grp_date); end if; end for; max_events = select * from my_tmp; end;
call max_by_24_hrs(?);
RECORD_NUMBER | CREATE_DATE |
567546 | 2015-07-28 10:45:24.0 |
567546 | 2015-07-29 17:13:26.0 |
567597 | 2015-07-26 11:34:29.0 |
567597 | 2015-07-28 13:12:43.0 |
567597 | 2015-07-29 18:39:51.0 |
This version at least avoids dynamic SQL and loops over the data only once.
Would be interesting to see if anyone finds a more elegant or better performing option.
- Lars