在 Oracle 中写一个递归函数 SQL
Write a recursive function in Oracle SQL
ORDER_ID TRANSACT_TIME
AAA 24-JUL-17 04.05.54.491000000 PM
AAA 24-JUL-17 04.05.54.496000000 PM
AAA 24-JUL-17 04.05.54.504000000 PM
BBB 24-JUL-17 04.05.54.491000000 PM
BBB 24-JUL-17 04.05.54.497000000 PM
BBB 24-JUL-17 04.05.54.505000000 PM
... ...
此数据(订单 ID 和 Transaction_Time)大约每小时发布到 table。
我正在尝试在 Oracle SQL 中创建一个函数(或 sql 查询)以确保每个后续事务时间都大于或等于前一个条目。
基本上 AAA 中的第二个条目的交易时间不能比 AAA 中的第一个条目更早,依此类推。
我想要一些关于如何编写此函数的指导。我发现的一个想法是编写一个递归函数,我尝试过类似的方法,但我不完全确定发生了什么:
Note: PREV_TRANS_TIME is an alias I generated to grab the transaction
time of the previous transaction (meaning for first entry PREV_TRANS_TIME = null)
CREATE OR REPLACE FUNCTION TransactTimeChecker(PREV_TRANS_TIME) RETURN STRING AS
BEGIN
IF prev_trans_time > transact_time THEN
RETURN (I'm not sure what to write
here but I need to fix the entries so the order is adjusted)
ELSIF prev_trans_time <= transact_time THEN
RETURN (we are good, no problem)
ELSE
--Do nothing
END IF;
END;
我认为您不需要递归。我假设 prev_trans_time 是上次使用的订购时间。您想等到获得新的时间再创建下一个事务。您的函数可以迭代,直到系统时间上升。
declare
transact_time date := sysdate;
begin
while (prev_trans_time > transact_time) loop
transact_time := sysdate;
end loop;
return transact_time;
end;
ORDER_ID TRANSACT_TIME
AAA 24-JUL-17 04.05.54.491000000 PM
AAA 24-JUL-17 04.05.54.496000000 PM
AAA 24-JUL-17 04.05.54.504000000 PM
BBB 24-JUL-17 04.05.54.491000000 PM
BBB 24-JUL-17 04.05.54.497000000 PM
BBB 24-JUL-17 04.05.54.505000000 PM
... ...
此数据(订单 ID 和 Transaction_Time)大约每小时发布到 table。
我正在尝试在 Oracle SQL 中创建一个函数(或 sql 查询)以确保每个后续事务时间都大于或等于前一个条目。
基本上 AAA 中的第二个条目的交易时间不能比 AAA 中的第一个条目更早,依此类推。
我想要一些关于如何编写此函数的指导。我发现的一个想法是编写一个递归函数,我尝试过类似的方法,但我不完全确定发生了什么:
Note: PREV_TRANS_TIME is an alias I generated to grab the transaction
time of the previous transaction (meaning for first entry PREV_TRANS_TIME = null)
CREATE OR REPLACE FUNCTION TransactTimeChecker(PREV_TRANS_TIME) RETURN STRING AS
BEGIN
IF prev_trans_time > transact_time THEN
RETURN (I'm not sure what to write
here but I need to fix the entries so the order is adjusted)
ELSIF prev_trans_time <= transact_time THEN
RETURN (we are good, no problem)
ELSE
--Do nothing
END IF;
END;
我认为您不需要递归。我假设 prev_trans_time 是上次使用的订购时间。您想等到获得新的时间再创建下一个事务。您的函数可以迭代,直到系统时间上升。
declare
transact_time date := sysdate;
begin
while (prev_trans_time > transact_time) loop
transact_time := sysdate;
end loop;
return transact_time;
end;