I have wrote one SQL function which takes one argument and return Table type.
SQL Function -
create or replace function get_some_data (p_val in number)
return my_tab_type_coll pipelined as
begin
FOR i in (select final_test_tb.*, PERIOD_BETWEEN_TWO_DATES(
FROM_TZ(CAST(from_date AS TIMESTAMP), 'UTC'),
FROM_TZ(CAST(to_date AS TIMESTAMP), 'UTC') as period
from (
select dl.id, dl.trip_id, dl.address, dl.from_date, dl.avg_speed, dl.high_speed,
case when tmp = 2 and nvl(lead(avg_speed) over (order by received_at asc), 1) = 0
then lead(to_date) over (order by received_at asc)
else to_date end to_date, tmp
from (
select test.*, case when avg_speed <> 0 then 1
when nvl(lag(avg_speed) over (order by received_at asc), 1) <> 0 then 2
when nvl(lead(avg_speed) over (order by received_at asc), 1) <> 0 then 3
end tmp
from test
where foo_id = p_val) dl
where tmp is not null) final_test_tb
where tmp in (1, 2)) loop
pipe row(my_tab_type(i.id, i.foo_id,
i.avg_speed, i.high_speed, i.distance, i.address_en, i.from_date, i.to_date));
end loop;
return;
end;
Here, I already created two types i.e row type - my_tab_type & table type -my_tab_type_coll
While fetching the records using sql command =>
select * from TABLE(get_some_data(10003));
Is it possible to construct rails model from this function? So that, it will be possible to apply filters and pagination for that model/table.