we have two tables account and Dept where acocunt level 1 will be coming from acount table and level 2 and level 3 account level coming from dept. table
Now based on the input we need to find the accountkey which is in both tables and it should display which accountkey based on source account id, db id and account level.
Input:
[112].[22].[1],[113].[23].[1],[245].[21].[2],[289].[20].[2],[301].[21].[3],
[304].[20].[3]
Description:
112 - Account id(level1 accountid or leve2 accountid or level3 accountid),
22 - Database id,
1 or 2 or 3 -account level
Eg:
Account table :
DIM_CUST_KEY level1 account id databaseid
1123 112 22
1234 113 23
Dept table:
DIM_CUST_KEY level2 account id level3 account id databaseid
1587 245 301 21
1576 289 304 20
What i have tried:
`create or replace function get_accountdetails (par_input in varchar2) return
varchar2 is
v_ret varchar2(20) := '';
begin
select dim_cust_key from dim_cust_acnt a
inner join dim_cust_dept d using (dim_cust_key)
where ( 1 = regexp_substr(par_input, '\d+', 1, 3)
and regexp_substr(par_input, '\d+', 1, 1) = level1_account_id
and regexp_substr(par_input, '\d+', 1, 2) = a.database_id )
or ( 2 = regexp_substr(par_input, '\d+', 1, 3)
and regexp_substr(par_input, '\d+', 1, 1) = level2_account_id
and regexp_substr(par_input, '\d+', 1, 2) = d.database_id )
or ( 3 = regexp_substr(par_input, '\d+', 1, 3)
and regexp_substr(par_input, '\d+', 1, 1) = level3_account_id
and regexp_substr(par_input, '\d+', 1, 2) = d.database_id )
return v_ret;
end;`
You seems to be a little chaotic in problem description, please format your question and make it more readable. Anyway the solutions is simple. You only need one
if
statement, where, depending on level, you search in first or second table and proper column:Here are tables and sample function calls:
Please use proper columns names which you have in your real data, and adjust variable types and length if needed. I think you could also use one joined query, no special function needed, something like below. I used
full join
, because your examples do not contain matching rows. Probably simplejoin
will be enough.Result:
If you remove
with
andcross join
parts and addinto
clause then you can use this query in function instead ofif
statement.Edit:
Sorry for delay, I did not look at Stack Overflow lately. Here are two examples how to write your functions:
This function returns concatenated string:
And second function is pipelined and returns data as predefined collecton of strings, so values are in separate rows.
You can also parse all input data at first, store them in some collection and then use bulk collect in one query. There is many solutions and possibilities, personally I would use pipelined function, but it depeneds on what form of output you need (collection or concatenated string). Also you can add
begin ... end
block and handle exceptionwhen no_data_found
. You can present special info then or break execution, it depends on what behavior is expected in such situation.Function 1:
Function 2: