Function 用于返回日期之间的数据的PL/SQL函数

Function 用于返回日期之间的数据的PL/SQL函数,function,plsql,Function,Plsql,我在创建函数时遇到了问题 我希望函数能够找到某个帐户在某个日期的租金 该函数采用2个参数rentacc number和rentdate varchar2 create or replace function get_rent(rentacc in number,rentdate in varchar2) return number as atype number :=rentacc begin if atype =1 then select "RATE" from "RENTCHANGE" w

我在创建函数时遇到了问题

我希望函数能够找到某个帐户在某个日期的租金

该函数采用2个参数rentacc number和rentdate varchar2

create or replace function get_rent(rentacc in number,rentdate in varchar2)
return number
as
atype number :=rentacc
begin
if atype =1 
then
select "RATE" from "RENTCHANGE" where TO_DATE(rentdate, 'YYYY-MM-DD') >= TIME or TO_DATE(rentdate, 'YYYY-MM-DD') <=TIME;
else return -1;
end if;
end get_rent;
会回来吗

function_name
-------------------------  
1,78
如果有人有任何建议,我将不胜感激。
谢谢。

您的问题令人困惑,因为您的示例数据和函数使用不同的列名等

无论如何,这里有一条SQL语句可以帮助您了解如何修改函数中的查询:

with rentchange as (select 123 id, 1 account, .58 rate, to_date('09/07/2013', 'dd/mm/yyyy') time from dual union all
                    select 124 id, 1 account, .69 rate, to_date('02/09/2013', 'dd/mm/yyyy') time from dual union all
                    select 125 id, 1 account, 1.78 rate, to_date('07/10/2013', 'dd/mm/yyyy') time from dual union all
                    select 126 id, 1 account, 2.7 rate, to_date('17/10/2013', 'dd/mm/yyyy') time from dual)
-- end of mimicking the rentchange table with data in it. See SQL below:
select rate
from   (select id,
               account,
               rate,
               time start_time,
               lead(time, 1, sysdate) over (partition by account
                                            order by time) end_time
        from   rentchange)
where  start_time <= to_date('10/10/2013', 'dd/mm/yyyy')
and    end_time > to_date('10/10/2013', 'dd/mm/yyyy');

      RATE
----------
      1.78

这将使用lead分析功能来提取关于下一行日期的信息,或者,如果没有下一行,则使用当前时间,这将为您提供一个可以查询的日期范围。

这可能是您所需要的,与以前的帖子相比要简化一点

create or replace function get_ränta(
p_kontotyp in number,
p_datum in varchar2)
return number
is
v_svar number(5,2);
begin


select ränta into v_svar 
from ränteändring 
where tid = (select max(tid) from ränteändring
             where tid <= to_date(p_datum, 'yyyy,mm,dd')
             and ktyp = p_kontotyp);

return v_svar;
exception
when no_data_found then
return -1;
end;

你确定这个函数就是列出的那个吗?不确定我是否遵循了,但是如果我想在我的表中再插入100行,我必须更改我的函数。这不是可选的,是吗?@ErikRehn如果你说的是rentchange子查询,我用来生成在sql主位中运行的数据,那么我希望你意识到你不需要它。因为您没有充分解释函数应该做什么以及如何使用它,所以几乎不可能说您是否需要更改它。我想说的是,如果你只打算为一个特定的帐户进行选择,那么一开始你可能没有设计好它!那么你在哪里声明开始和停止时间,我将使用我的参数?嗯,不;这些是查询中的列;这是截止日期“2013年10月10日”,“dd/mm/yyyy”,将被参数替换。哇!很不错的。干得好
with rentchange as (select 123 id, 1 account, .58 rate, to_date('09/07/2013', 'dd/mm/yyyy') time from dual union all
                    select 124 id, 1 account, .69 rate, to_date('02/09/2013', 'dd/mm/yyyy') time from dual union all
                    select 125 id, 1 account, 1.78 rate, to_date('07/10/2013', 'dd/mm/yyyy') time from dual union all
                    select 126 id, 1 account, 2.7 rate, to_date('17/10/2013', 'dd/mm/yyyy') time from dual)
-- end of mimicking the rentchange table with data in it. See SQL below:
select rate
from   (select id,
               account,
               rate,
               time start_time,
               lead(time, 1, sysdate) over (partition by account
                                            order by time) end_time
        from   rentchange)
where  start_time <= to_date('10/10/2013', 'dd/mm/yyyy')
and    end_time > to_date('10/10/2013', 'dd/mm/yyyy');

      RATE
----------
      1.78
create or replace function get_ränta(
p_kontotyp in number,
p_datum in varchar2)
return number
is
v_svar number(5,2);
begin


select ränta into v_svar 
from ränteändring 
where tid = (select max(tid) from ränteändring
             where tid <= to_date(p_datum, 'yyyy,mm,dd')
             and ktyp = p_kontotyp);

return v_svar;
exception
when no_data_found then
return -1;
end;