Problem using Model Clause
Hi,
I am executing following query and get the output as shown below.
Select distinct(Entry_time), Paymentmode_code
, shift_date
, vehicleclass_code,
Cnt
from v_wog_summary
model
UNIQUE SINGLE REFERENCE
partition by (Entry_Time)
dimension by (paymentmode_code )
measures (vehicleclass_code,shift_date, 0 as Cnt)
RULES upsert
Cnt[for paymentmode_code in (Select distinct(paymentmode_code) from v_wog_summary)] = 1
order by shift_date,entry_time,paymentmode_code
Actual Output:
ENTRY_TIME PAYMENTMODE_CODE SHIFT_DATE VEHICLECLASS_CODE CNT
0:00 2 2-Jan-11 2 1
0:00 3 2-Jan-11 3 1
0:00 5 2-Jan-11 5 1
1:00 1 2-Jan-11 1 1
1:00 2 2-Jan-11 2 1
1:00 3 2-Jan-11 3 1
2:00 1 2-Jan-11 1 1
2:00 2 2-Jan-11 2 1
2:00 3 2-Jan-11 3 1
2:00 4 2-Jan-11 4 1
I require the following output of the above query:
the query should display null in vechileclass_code if it is not present.
Also the query should display all the paymentmode_code (in my case there are total 14 paymentmode_code)
Required Output :
ENTRY_TIME PAYMENTMODE_CODE SHIFT_DATE VEHICLECLASS_CODE CNT
0:00 1 2-Jan-11 null 1
0:00 2 2-Jan-11 2 1
0:00 3 2-Jan-11 3 1
0:00 4 2-Jan-11 null 1
0:00 5 2-Jan-11 1 1
0:00 6 2-Jan-11 null 1
0:00 7 2-Jan-11 null 1
0:00 8 2-Jan-11 null 1
0:00 9 2-Jan-11 null 1
0:00 10 2-Jan-11 null 1
0:00 11 2-Jan-11 null 1
0:00 12 2-Jan-11 null 1
0:00 13 2-Jan-11 null 1
0:00 14 2-Jan-11 null 1
1:00 1 2-Jan-11 1 1
1:00 2 2-Jan-11 2 1
1:00 3 2-Jan-11 3 1
1:00 4 2-Jan-11 null 1
1:00 5 2-Jan-11 null 1
1:00 6 2-Jan-11 null 1
1:00 7 2-Jan-11 null 1
1:00 8 2-Jan-11 null 1
1:00 9 2-Jan-11 null 1
1:00 10 2-Jan-11 null 1
1:00 11 2-Jan-11 null 1
1:00 12 2-Jan-11 null 1
1:00 13 2-Jan-11 null 1
1:00 14 2-Jan-11 null 1
2:00 1 2-Jan-11 1 1
2:00 2 2-Jan-11 2 1
2:00 3 2-Jan-11 3 1
2:00 4 2-Jan-11 5 1
2:00 5 2-Jan-11 null 1
2:00 6 2-Jan-11 null 1
2:00 7 2-Jan-11 null 1
2:00 8 2-Jan-11 null 1
2:00 9 2-Jan-11 null 1
2:00 10 2-Jan-11 null 1
2:00 11 2-Jan-11 null 1
2:00 12 2-Jan-11 null 1
2:00 13 2-Jan-11 null 1
2:00 14 2-Jan-11 null 1
I am not pretty sure what you want, but i think it is some kind of row generator. For thsi you can use the for loop
with v_wog_summary as (
select
to_date('02012011','DDMMYYYY') shift_date
, to_char(level-1)||':00' Entry_time
, level*2 paymentmode_code
, level*3 vehicleclass_code
from dual
connect by
level <= 3
select
shift_date
,Entry_time
,p paymentmode_code
,v vehicleclass_code
from v_wog_summary
model
partition by (shift_date, entry_time)
dimension by (paymentmode_code p)
measures(vehicleclass_code v)
rules(
v[for p from 1 to 14 increment 1]=v[cv()]
order by shift_date, entry_time, p
SHIFT_DATE ENTRY_TIME PAYMENTMODE_CODE VEHICLECLASS_CODE
01/02/2011 0:00 1 -
01/02/2011 0:00 2 3
01/02/2011 0:00 3 -
01/02/2011 0:00 4 -
01/02/2011 0:00 5 -
01/02/2011 0:00 6 -
01/02/2011 0:00 7 -
01/02/2011 0:00 8 -
01/02/2011 0:00 9 -
01/02/2011 0:00 10 -
01/02/2011 0:00 11 -
01/02/2011 0:00 12 -
01/02/2011 0:00 13 -
01/02/2011 0:00 14 -
01/02/2011 1:00 1 -
01/02/2011 1:00 2 -
01/02/2011 1:00 3 -
01/02/2011 1:00 4 6
01/02/2011 1:00 5 -
01/02/2011 1:00 6 -
01/02/2011 1:00 7 -
01/02/2011 1:00 8 -
01/02/2011 1:00 9 -
01/02/2011 1:00 10 -
01/02/2011 1:00 11 -
01/02/2011 1:00 12 -
01/02/2011 1:00 13 -
01/02/2011 1:00 14 -
01/02/2011 2:00 1 -
01/02/2011 2:00 2 -
01/02/2011 2:00 3 -
01/02/2011 2:00 4 -
01/02/2011 2:00 5 -
01/02/2011 2:00 6 9
01/02/2011 2:00 7 -
01/02/2011 2:00 8 -
01/02/2011 2:00 9 -
01/02/2011 2:00 10 -
01/02/2011 2:00 11 -
01/02/2011 2:00 12 -
01/02/2011 2:00 13 -
01/02/2011 2:00 14 -
Similar Messages
-
Avoiding null and duplicate values using model clause
Hi,
I am trying to use model clause to get comma seperated list of data : following is the scenario:
testuser>select * from test1;
ID VALUE
1 Value1
2 Value2
3 Value3
4 Value4
5 Value4
6
7 value5
8
8 rows selected.
the query I have is:
testuser>with src as (
2 select distinct id,value
3 from test1
4 ),
5 t as (
6 select distinct substr(value,2) value
7 from src
8 model
9 ignore nav
10 dimension by (id)
11 measures (cast(value as varchar2(100)) value)
12 rules
13 (
14 value[any] order by id =
15 value[cv()-1] || ',' || value[cv()]
16 )
17 )
18 select max(value) oneline
19 from t;
ONELINE
Value1,Value2,Value3,Value4,Value4,,value5,
what I find is that this query has duplicate value and null (',,') coming in as data has null and duplicate value. Is there a way i can avoid the null and the duplicate values in the query output?
thanks,
Edited by: orausern on Feb 19, 2010 5:05 AMHi,
Try this code.
with
t as ( select substr(value,2)value,ind
from test1
model
ignore nav
dimension by (id)
measures (cast(value as varchar2(100)) value, 0 ind)
rules
( ind[any]= instr(value[cv()-1],value[cv()]),
value[any] order by id = value[cv()-1] || CASE WHEN value[cv()] IS NOT NULL
and ind[cv()]=0 THEN ',' || value[cv()] END
select max(value) oneline
from t;
SQL> select * from test1;
ID VALUE
1 Value1
2 Value2
3 Value3
4 Value4
5 Value4
6
7 value5
8
8 ligne(s) sélectionnée(s).
SQL> with
2 t as ( select substr(value,2)value,ind
3 from test1
4 model
5 ignore nav
6 dimension by (id)
7 measures (cast(value as varchar2(100)) value, 0 ind)
8 rules
9 ( ind[any]= instr(value[cv()-1],value[cv()]),
10 value[any] order by id = value[cv()-1] || CASE WHEN value[cv()] IS NOT NULL
11 and ind[cv()]=0 THEN ',' || value[cv()] END
12 )
13 )
14 select max(value) oneline
15 from t;
ONELINE
Value1,Value2,Value3,Value4,value5
SQL> -
Unable to display data no entry in the table without using Model clause
Hi,
I've an urgent requirement described below :
The previously posted Question has been answerted using Model Clause:
Is there any way out to solve it without using Model clause:
I've a table named as "sale" consisting of three columns : empno, sale_amt and sale_date.
(Please ref. The table script with data as given below)
Now if I execute the query :
"select trunc(sale_date) sale_date, sum(sale_amt) total_sale from sale group by trunc(sale_date) order by 1"
then it displays the data for the dates of which there is an entry in that table. But it does not display data for the
date of which there is no entry in that table.
If you run the Table script with data in your schema, then u'll see that there is no entry for 28th. Nov. 2009 in
sale table. Now the above query displays data for rest of the dates as its are in sale table except for 28th. Nov. 2009.
But I need its presence in the query output with a value of "sale_date" as "28th. Nov. 2009" and that of "total_sale" as
"0".
Is there any means to get the result as I require?
Please help ASAP.
Thanks in advance.
Create table script with data:
CREATE TABLE SALE
EMPNO NUMBER,
SALE_AMT NUMBER,
SALE_DATE DATE
SET DEFINE OFF;
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(100, 1000, TO_DATE('12/01/2009 10:20:10', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(100, 1000, TO_DATE('11/30/2009 10:21:04', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(100, 1000, TO_DATE('11/29/2009 10:21:05', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(100, 1000, TO_DATE('11/26/2009 10:21:06', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(100, 1000, TO_DATE('11/25/2009 10:21:07', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(200, 5000, TO_DATE('11/27/2009 10:23:06', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(200, 4000, TO_DATE('11/29/2009 10:23:08', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(200, 3000, TO_DATE('11/24/2009 10:23:09', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(200, 2000, TO_DATE('11/30/2009 10:23:10', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(300, 7000, TO_DATE('11/24/2009 10:24:19', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(300, 5000, TO_DATE('11/25/2009 10:24:20', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(300, 3000, TO_DATE('11/27/2009 10:24:21', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(300, 2000, TO_DATE('11/29/2009 10:24:22', 'MM/DD/YYYY HH24:MI:SS'));
Insert into SALE
(EMPNO, SALE_AMT, SALE_DATE)
Values
(300, 1000, TO_DATE('11/30/2009 10:24:22', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;
Any help will be needful for me
Regards,select sale_date,sum(sale_amt) total_sale
from
select empno,0 sale_amt,(sale_date + ao.rn) sale_date
from
select empno,sale_amt,sale_date ,(t.nxt_dt - t.sale_date) diff
from
select empno
,sale_amt,trunc(sale_date) sale_date
,trunc(nvl(lead(sale_date) over (partition by 1 order by sale_date),sale_date)) nxt_dt
from sale
) t
where (t.nxt_dt - t.sale_date) >1
) rec,(select rownum rn from user_objects where rownum<=200) ao
where ao.rn <=(rec.diff-1)
union all
select empno,sale_amt,trunc(sale_date) sale_date
from sale
group by sale_date
order by 1;
~~~~Guess this will serve the purpose...
Cheers Arpan -
Pivoting using Model Clause Vs pivoting using Aggregate Fun(Case) statement
Hi,
I just wanted to know which option is better for pivoting the data if the data is like:
Grp1 Grp2 Day_week Sales
1 1 Sun 200
1 1 Mon 100
1 2 Sun 1000
and so on...
The output should be:
Grp1 Grp2 Sun Mon ......
1 1 200 100.....
1 2 1000 NULL....
I have tested the same using sum(decode...) method and also model clause:
select grp1, grp2, sum(decode(day_week,'SUN',sales)) SUNDAY , sum(decode(day_week,'MON',sales)) MONDAY from pivot
group by grp1, grp2
order by grp1, grp2
select grp1, grp2, sun , mon, tue, wed, thr, fri, sat
from pivot
model -- UNIQUE SINGLE REFERENCE
return updated rows
partition by (grp1, grp2)
dimension by ( day_week)
measures( result, 0 mon, 0 tue, 0 wed, 0 thr,0 fri, 0 sat, 0 sun)
RULES upsert
mon[0]= sales['MON'],
tue[0]= sales['TUE'],
wed[0]= sales['WED'],
thr[0]= sales['THR'],
fri[0]= sales['FRI'],
sat[0]= sales['SAT'],
sun[0]= sales['SUN']
) order by grp1, grp2
There are 4K records in the table. The first query is taking app (.125 seconds) and the second query (.230 seconds).
Pls tell how the model clause can give better performance and I want to use it for pivoting in all my APIs.
Regards
Rajiv> I read somewhere while searching on net.
And now you can't find it anymore? I wouldn't believe anything you read somewhere while searching on net, unless it has some kind of proof attached.
> You pls tell is it so or it depends upon volume of data.
Also i tested on some data and reults were better in
case of using normal startegy rather than model.(case
provided above).
So you have tested it yourself already. The model clause is great in a few cases (string aggregation, calculating values based on calculated values), but this doesn't seem to be one of them. On 11g you might want to consider the PIVOT operator.
Regards,
Rob. -
Using MODEL clause and COUNT for not numeric data columns....
Hi ,
Is it possible somehow to use the COUNT function to transform a non-numeric data column to a numeric data value (a counter) and be used in a MODEL clause....????
For example , i tried the following in the emp table of SCOTT dataschema with no desired result...
SQL> select deptno , empno , hiredate from emp;
DEPTNO EMPNO HIREDATE
20 7369 18/12/1980
30 7499 20/02/1981
30 7521 22/02/1981
20 7566 02/04/1981
30 7654 28/09/1981
30 7698 01/05/1981
10 7782 09/06/1981
20 7788 18/04/1987
10 7839 17/11/1981
30 7844 08/09/1981
20 7876 21/05/1987
30 7900 03/12/1981
20 7902 03/12/1981
10 7934 23/01/1982
14 rows selected Now , i want to use the MODEL clause in order to 'predict' the number of employees who were going to be hired in the 1990 per deptno...
So , i have constructed the following query which , as expected, does not return the desired results....
SQL> select deptno , month , year , count_
2 from
3 (
4 select deptno , to_number(to_char(hiredate,'mm')) month ,
5 to_number(to_char(hiredate , 'rrrr')) year , count(ename) count_
6 from emp
7 group by deptno , to_number(to_char(hiredate,'mm')) ,
8 to_number(to_char(hiredate , 'rrrr'))
9 )
10 model
11 partition by(deptno)
12 dimension by (month , year)
13 measures (count_ )
14 (
15 count_[1,1990]=count_[1,1982]+count_[11,1982]
16 )
17 /
DEPTNO MONTH YEAR COUNT_
30 5 1981 1
30 12 1981 1
30 2 1981 2
30 9 1981 2
30 1 1990
20 4 1987 1
20 5 1987 1
20 4 1981 1
20 12 1981 1
20 12 1980 1
20 1 1990
10 6 1981 1
10 11 1981 1
10 1 1982 1
10 1 1990 As you see , the measures for the 1990 year is null...because the measure(the count(deptno)) is computed via the group by and not by the MODEL clause...
How should i transform the above query... so as the "count_[1,1982]+count_[11,1982]" will return non-null results per deptno...????
Thanks , a lot
SimonConnected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL>
SQL> SELECT department_id, MONTH, YEAR, count_
2 FROM (SELECT e.department_id
3 ,to_number(to_char(e.hire_date, 'mm')) MONTH
4 ,to_number(to_char(e.hire_date, 'rrrr')) YEAR
5 ,COUNT(e.first_name) count_
6 FROM employees e
7 WHERE e.department_id = 20
8 GROUP BY e.department_id
9 ,to_number(to_char(e.hire_date, 'mm'))
10 ,to_number(to_char(e.hire_date, 'rrrr')));
DEPARTMENT_ID MONTH YEAR COUNT_
20 8 1997 1
20 2 1996 1
SQL> --
SQL> SELECT department_id, MONTH, YEAR, count_
2 FROM (SELECT e.department_id
3 ,to_number(to_char(e.hire_date, 'mm')) MONTH
4 ,to_number(to_char(e.hire_date, 'rrrr')) YEAR
5 ,COUNT(e.first_name) count_
6 FROM employees e
7 WHERE e.department_id = 20
8 GROUP BY e.department_id
9 ,to_number(to_char(e.hire_date, 'mm'))
10 ,to_number(to_char(e.hire_date, 'rrrr')))
11 model
12 PARTITION BY(department_id)
13 dimension BY(MONTH, YEAR)
14 measures(count_)(
15 count_ [1, 1990] = count_ [2, 1996] + count_ [8, 1997]
16 );
DEPARTMENT_ID MONTH YEAR COUNT_
20 8 1997 1
20 2 1996 1
20 1 1990 2
SQL> ---
SQL> SELECT department_id, MONTH, YEAR, count_
2 FROM (SELECT e.department_id
3 ,to_number(to_char(e.hire_date, 'mm')) MONTH
4 ,to_number(to_char(e.hire_date, 'rrrr')) YEAR
5 ,COUNT(e.first_name) count_
6 FROM employees e
7 GROUP BY e.department_id
8 ,to_number(to_char(e.hire_date, 'mm'))
9 ,to_number(to_char(e.hire_date, 'rrrr')))
10 model ignore nav
11 PARTITION BY(department_id)
12 dimension BY(MONTH, YEAR)
13 measures(count_)(
14 count_ [1, 1990] = count_ [2, 1996] + count_ [8, 1997]
15 );
DEPARTMENT_ID MONTH YEAR COUNT_
100 8 1994 2
30 12 1997 1
100 3 1998 1
30 7 1997 1
5 1999 1
30 12 1994 1
30 11 1998 1
30 5 1995 1
100 9 1997 2
100 12 1999 1
30 8 1999 1
1 1990 0
30 1 1990 0
100 1 1990 0
90 9 1989 1
20 8 1997 1
70 6 1994 1
93 rows selected
SQL> -
I'm making a procedure, and it has a cursor. This cursor receive some parameters and one of them is a list of numbers, the list can't be a VARCHAR2 with ',' or ';' as separator, because it doesn't work, if I use a defined type like 'vet IS TABLE OF NUMBER INDEX BY BINARY_INTEGER' it doesn't work either. The cursor is necessary because it return many output. Here is my code:
PROCEDURE pr_busca_equipamento (
id_pos IN NUMBER,
tipo_posicao IN VARCHAR2,
empresa IN NUMBER,
nro_serie IN VARCHAR2,
id_item IN NUMBER,
status IN NUMBER,
id_equipamento OUT vetornum, -- table of number...
desc_equip OUT vetorchar, -- table of varchar2..
caminho_completo OUT vetorchar,
nro_serie_out OUT vetorchar,
status_out OUT vetornum,
quantidade OUT NUMBER,
erro_cod OUT VARCHAR2,
erro_msg OUT VARCHAR2
IS
CURSOR c_busca_equip(
id_p NUMBER,
tipo_p VARCHAR2,
emp NUMBER,
nroser VARCHAR2, -- or vetorchar
id_i NUMBER,
stat NUMBER)
IS
SELECT eq.id_equipamento,
nroserie,
id_status,
id_item
FROM EQUIPAMENTOS EQ,
EQUIPAMENTOS_POSICAO EP
WHERE (eq.id_equipamento = ep.id_equipamento AND ep.id_posicao = id_p)
AND (id_i IS NULL OR id_item = id_i)
AND (stat IS NULL OR id_status = stat)
AND (nroser IS NULL OR nroserie IN (nroser)) -- my problem is nroser
AND (emp IS NULL OR id_empresa_responsavel = emp);
...Marcelo,
You can make this work using a VARCHAR2 list separated with commas if you write a 'dynamic' cursor.
For example:
PROCEDURE process_emp (p_deptno IN VARCHAR2)
IS
TYPE my_cur_type IS REF CURSOR; -- could be declared as a strong refcursor (e.g. RETURN emp%ROWTYPE)
my_cur my_cur_type;
emp_rec emp%ROWTYPE; --or declare the appropriate record structure
BEGIN
OPEN my_cur FOR 'select * from emp where deptno in ('||p_deptno||')';
LOOP
FETCH my_cur INTO emp_rec;
EXIT WHEN my_cur%NOTFOUND;
-- process the row returned from the cursor e.g.
htp.p(emp_rec.ename||' earns '||to_char(emp_rec.sal,'fm$999,999'));
END LOOP;
CLOSE my_cur;
END;
Hope this helps.
Cheers,
Bryan. -
How to use model clause without hard coding the values in it?
Query
select acct_no,
gl_code,
CASE
WHEN entry_type_label IN ('Earned Revenue') THEN
'Earned Revenue'
ELSE
'Deferred Revenue Credit'
END AS entry_type_label,
CASE
WHEN entry_type_label IN ('Opening Balance') THEN
'Opening Balance'
WHEN entry_type_label IN ('Deferred Revenue Credit') THEN
'Invoice Amount'
WHEN entry_type_label IN ('Earned Revenue') THEN
'Earned Revenue'
WHEN entry_type_label IN ('Closing Balance') THEN
'Closing Balance'
ELSE
'Deferred Revenue Credit'
END AS label,
entry_type_no,
orig_chg_start_date,
period_no,
-amt as amt
from revrec_test
WHERE acct_no = 1788562
AND entry_type_no IN (2, 4) model dimension by(acct_no,
gl_code,
entry_type_label,
entry_type_no,
orig_chg_start_date,
period_no) measures(amt) rules upsert
all(amt 1788562,
'UNEARNED-10011561',
'Opening Balance',
2,
'02-OCT-17 08.30.00 AM',
190 = 0,
amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
190 = amt 1788562,
'UNEARNED-10011561',
'Deferred Revenue Credit',
2,
'02-OCT-17 08.30.00 AM',
190 - amt 1788562,
'EARNED-10011561',
'Earned Revenue',
4,
'02-OCT-17 08.30.00 AM',
190,
amt 1788562,
'UNEARNED-10011561',
'Opening Balance',
2,
'02-OCT-17 08.30.00 AM',
191 = amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
190,
amt 1788562,
'UNEARNED-10011561',
'Deferred Revenue Credit',
2,
'02-OCT-17 08.30.00 AM',
191 = 0,
amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
191 =
(amt 1788562, 'UNEARNED-10011561', 'Opening Balance', 2,
'02-OCT-17 08.30.00 AM', 191 + amt 1788562, 'UNEARNED-10011561',
'Deferred Revenue Credit', 2, '02-OCT-17 08.30.00 AM', 191) - amt
1788562,
'EARNED-10011561',
'Earned Revenue',
4,
'02-OCT-17 08.30.00 AM',
191,
amt 1788562,
'UNEARNED-10011561',
'Opening Balance',
2,
'02-OCT-17 08.30.00 AM',
192 = amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
191,
amt 1788562,
'UNEARNED-10011561',
'Deferred Revenue Credit',
2,
'02-OCT-17 08.30.00 AM',
192 = 0,
amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
192 =
(amt 1788562, 'UNEARNED-10011561', 'Opening Balance', 2,
'02-OCT-17 08.30.00 AM', 192 + amt 1788562, 'UNEARNED-10011561',
'Deferred Revenue Credit', 2, '02-OCT-17 08.30.00 AM', 192) - amt
1788562,
'EARNED-10011561',
'Earned Revenue',
4,
'02-OCT-17 08.30.00 AM',
192,
amt 1788562,
'UNEARNED-10011561',
'Opening Balance',
2,
'02-OCT-17 08.30.00 AM',
193 = amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
192,
amt 1788562,
'UNEARNED-10011561',
'Deferred Revenue Credit',
2,
'02-OCT-17 08.30.00 AM',
193 = 0,
amt 1788562,
'UNEARNED-10011561',
'Closing Balance',
2,
'02-OCT-17 08.30.00 AM',
193 =
(amt 1788562, 'UNEARNED-10011561', 'Opening Balance', 2,
'02-OCT-17 08.30.00 AM', 193 + amt 1788562, 'UNEARNED-10011561',
'Deferred Revenue Credit', 2, '02-OCT-17 08.30.00 AM', 193) - amt
1788562,
'EARNED-10011561',
'Earned Revenue',
4,
'02-OCT-17 08.30.00 AM',
193)
ORDER BY period_no, entry_type_no;
The above query works fine. But i have hard coded the values. I want to do the same operation for different account number which is going to have different periodic no. How can I achieve it?
Thanks in advance.Create Statement
CREATE TABLE table_one(
ACCT_NO NUMBER(38),
GL_CODE VARCHAR2(300),
ENTRY_TYPE_LABEL CHAR(50),
ENTRY_TYPE_NO NUMBER,
ORIG_CHG_START_DATE TIMESTAMP(0) WITH LOCAL TIME ZONE,
ORIG_CHG_END_DATE TIMESTAMP(0) WITH LOCAL TIME ZONE,
PERIOD_NO NUMBER(38),
AMT NUMBER(38,10)
Insert Statement
INSERT ALL
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'U-11561','Deferred Revenue Debit',3,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',100,13.87)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'E-11561','Earned Revenue',4,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',102,-14.83)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'E-11561','Earned Revenue',4,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',101,-14.35)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'E-11561','Earned Revenue',4,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',100,-13.87)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'U-11561','Deferred Revenue Debit',3,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',103,0.95)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'U-11561','Deferred Revenue Debit',3,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',102,14.83)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'U-11561','Deferred Revenue Debit',3,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',101,14.35)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'E-11561','Earned Revenue',4,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',103,-0.95)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1001,'U-11561','Deferred Revenue Credit',2,'02-OCT-17 08.30.00 AM','01-JAN-18 01.30.00 PM',100,-44)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,44.91)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Accounts Receivable',1,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',104,60)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,3.93)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,11.75)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,6.86)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,-7.82)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,-23.47)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,-13.69)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,-6.86)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,7.82)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,23.47)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,13.69)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',104,-9.13)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,-44.91)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,-44.91)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,-35.91)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,-3.93)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,44.91)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',104,9.13)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Earned Revenue',4,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',106,-11.75)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Credit',2,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,38.48)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Credit',2,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,-19.24)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Credit',2,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',104,-60)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Accounts Receivable',1,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,38.48)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Accounts Receivable',1,'20-DEC-13 09.30.00 AM','16-FEB-14 09.30.00 AM',105,19.24)
INTO table_one(acct_no, gl_code, entry_type_label, entry_type_no, orig_chg_start_date, orig_chg_end_date, period_no, amt) VALUES(1002,'Recurring Flat Fees COA Code','Deferred Revenue Debit',3,'17-NOV-13 09.30.00 AM','16-FEB-14 09.30.00 AM',107,35.91)
SELECT * FROM dual;
Expected Result
S.NO
ACCT_NO
GL_CODE
ENTRY_TYPE_LABEL
ENTRY_TYPE_NO
ORIG_CHG_START_DATE
PERIOD_NO
AMT
DESCRIPTION
1
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
100
0
Opening Account
2
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
100
44
Invoice
3
1001
E-11561
Earned Revenue
4
02-OCT-17 08.30.00 AM
100
13.87
Invoice Paid
4
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
100
30.13
Closing Account
5
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
101
30.13
Opening Account
6
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
101
0
Invoice
7
1001
E-11561
Earned Revenue
4
02-OCT-17 08.30.00 AM
101
14.35
Invoice Paid
8
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
101
15.78
Closing Account
9
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
102
15.78
Opening Account
10
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
102
0
Invoice
11
1001
E-11561
Earned Revenue
4
02-OCT-17 08.30.00 AM
102
14.83
Invoice Paid
12
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
102
0.95
Closing Account
13
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
103
0.95
Opening Account
14
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
103
0
Invoice
15
1001
E-11561
Earned Revenue
4
02-OCT-17 08.30.00 AM
103
0.95
Invoice Paid
16
1001
U-11561
Deferred Revenue Credit
2
02-OCT-17 08.30.00 AM
103
0
Closing Account
Description
We must start opening account as zero and end with closing account as zero by doing manipulation with invoice and invoice paid.
Process
Initial Stage
Subsequent stage
Opening Account
0
(=closing Account)
Invoice
max value of account no which has entry type no 2
0
Invoice Paid
taken from the field amount which has entry type no 4
taken from the field amount which has entry type no 4
Closing Account
(=[opening account + invoice] - invoice paid)
(=[opening account + invoice] - invoice paid)
Note
1) Each account may have different periodic no.
2) Some account may have more than 4 distinct period no and less than 4 distinct periodic no.
3) Description column from expected result is not an part of table. It is been added for easier understanding. -
Hi Gurus,
Can someone please help me out.
I've a below tables.
1) tbl_link --> this table contains information at profile level
2) tbl_summary --> this table contains summary at parent profile level derived from tbl_link table
One parent profile contains multiple child profiles and each child profile links to a code (which is B, W, G or P) and the code is linked to a category (i.e. ONL and OFL). In this case code B is linked to category 'ONL' and codes W,G,P linked to OFL category.
ONL category needs 100 points. If it don't have enough points then i need to borrow from OFL category which i'm doing and populating into tbl_summary table at parent profile level.
Now i need to insert data into tbl_link table at profile level with howmany points used, expired based on tbl_summary table. Rule is at the end of month if we add points for each profile in tbl_link table it should come as 0.
with
tbl_SUMMARY as
select 1 as ppid,'ONL' as catgcode, 53 as earned_points,47 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 1 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,176 CERT_POINTS,76 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'ONL' as catgcode, 39 as earned_points,61 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'OFL' as catgcode, 90 as earned_points,0 BORROWED_POINTS,29 CERT_POINTS,29 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,223 CERT_POINTS,23 DISCARD_POINTS,200 used from dual
union
select 4 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 4 as ppid,'OFL' as catgcode, 169 as earned_points,0 BORROWED_POINTS,169 CERT_POINTS,69 DISCARD_POINTS,100 used from dual
tbl_link as
select 1 as ppid,1 as pid, 'B' as code,'ONL' as catgcode, 53 as earned_points from dual
union
select 1 as ppid,12 as pid, 'W' as code,'OFL' as catgcode, 26 as earned_points from dual
union
select 1 as ppid,13 as pid, 'G' as code,'OFL' as catgcode, 87 as earned_points from dual
union
select 1 as ppid,14 as pid, 'P' as code,'OFL' as catgcode, 110 as earned_points from dual
union
select 2 as ppid,2 as pid, 'B' as code,'ONL' as catgcode, 39 as earned_points from dual
union
select 2 as ppid,22 as pid, 'W' ,'OFL' as catgcode, 30 as earned_points from dual
union
select 2 as ppid,23 as pid, 'G' ,'OFL' as catgcode, 29 as earned_points from dual
union
select 2 as ppid,24 as pid, 'P' ,'OFL' as catgcode, 31 as earned_points from dual
union
select 3 as ppid,3 as pid, 'B' as code,'ONL' as tier_catgcode, 109 as earned_points from dual
union
select 3 as ppid,32 as pid, 'W' ,'OFL' , 26 as earned_points from dual
union
select 3 as ppid,33 as pid, 'G' ,'OFL', 87 as earned_points from dual
union
select 3 as ppid,34 as pid, 'P' ,'OFL' , 110 as earned_points from dual
union
select 4 as ppid,4 as pid, 'B' as code,'ONL' as catgcode, 109 as earned_points from dual
union
select 4 as ppid,42 as pid, 'W' as code,'OFL' , 26 as earned_points from dual
union
select 4 as ppid,43 as pid, 'G' as code,'OFL' , 87 as earned_points from dual
union
select 4 as ppid,44 as pid, 'P' as code,'OFL' , 56 as earned_points from dual
final (PARENT_PROFILE_ID,PROFILE_ID,catgcode,EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED)
as (
select A.PPID PARENT_PROFILE_ID,B.PID PROFILE_ID,A.catgcode,B.EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED
from tbl_SUMMARY a,tbl_link b where a.ppid=b.ppid AND A.catgcode=B.catgcode
ORDER BY PROFILE_ID
select * from final order by 1;
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED
1 1 ONL 53 47 100 0 100
1 14 OFL 110 0 176 76 100
1 13 OFL 87 0 176 76 100
1 12 OFL 26 0 176 76 100
2 2 ONL 39 61 100 0 100
2 24 OFL 31 0 29 29 100
2 23 OFL 29 0 29 29 100
2 22 OFL 30 0 29 29 100
3 32 OFL 26 0 223 23 200
3 33 OFL 87 0 223 23 200
3 34 OFL 110 0 223 23 200
3 3 ONL 109 0 109 9 100
4 42 OFL 26 0 169 69 100
4 43 OFL 87 0 169 69 100
4 44 OFL 56 0 169 69 100
4 4 ONL 109 0 109 9 100
Need Output as below :
For parent profile 1, whatever i mentioned above is not correct. Borrowed 47 points from OFL to ONL to make ONL and also from OFL category has 176 points remaining after lending to ONL and using only 100 points, remaining 76 points are discarded. Need to deduct these 76 points also from child profiles. Output will be as below.
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
1 1 ONL 53 47 100 0 100 -53 0
1 12 OFL 26 0 176 76 100 -26 0
1 13 OFL 87 0 176 76 100 -74 -13
1 14 OFL 110 0 176 76 100 -47 -63
For parent profile id 2 --> ONL category has 39 points, so borrowed 61 points from OFL category to make ONL points 100.
Now need to populate tbl_link table at child profile level (i.e. child profiles 22,23,24).
Borrowed 61 points from OFL and need to deduct this points from the profile which has highest earned points, in this case deduct from profile 24 which has 31 points, from profile 22 which has 30 points. Need output like below
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
2 2 ONL 39 61 100 0 100 -39 0
2 22 OFL 30 0 29 29 100 -30 0
2 23 OFL 29 0 29 29 100 0 -29
2 24 OFL 31 0 29 29 100 -31 0
For parent profile id 3 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 32,33,34).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 223 points total. need only 200 points (i.e. mutiple of 100) for our process, 23 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 34. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
3 3 ONL 109 0 109 9 100 -100 -9
3 32 OFL 26 0 223 23 200 -26 0
3 33 OFL 87 0 223 23 200 -87 0
3 34 OFL 110 0 223 23 200 -87 -23
For parent profile id 4 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 42,43,44).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 169 points total. need only 100 points (i.e. mutiple of 100) for our process, 69 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 43. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
4 4 ONL 109 0 109 9 100 100 9
4 42 OFL 26 0 169 69 100 -26 0
4 43 OFL 87 0 169 69 100 -18 -69
4 44 OFL 56 0 169 69 100 -56 0
Can someone help with the query. I googled about looping in sql and came to know that Oracle has a feature MODEL to loop in SQL, but i don't have idea on using MODEL clause.
Appreciate your help!
Thanks
SriHi Gurus,
Can someone please help me out.
I've a below tables.
1) tbl_link --> this table contains information at profile level
2) tbl_summary --> this table contains summary at parent profile level derived from tbl_link table
One parent profile contains multiple child profiles and each child profile links to a code (which is B, W, G or P) and the code is linked to a category (i.e. ONL and OFL). In this case code B is linked to category 'ONL' and codes W,G,P linked to OFL category.
ONL category needs 100 points. If it don't have enough points then i need to borrow from OFL category which i'm doing and populating into tbl_summary table at parent profile level.
Now i need to insert data into tbl_link table at profile level with howmany points used, expired based on tbl_summary table. Rule is at the end of month if we add points for each profile in tbl_link table it should come as 0.
with
tbl_SUMMARY as
select 1 as ppid,'ONL' as catgcode, 53 as earned_points,47 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 1 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,176 CERT_POINTS,76 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'ONL' as catgcode, 39 as earned_points,61 BORROWED_POINTS,100 CERT_POINTS,0 DISCARD_POINTS,100 used from dual
union
select 2 as ppid,'OFL' as catgcode, 90 as earned_points,0 BORROWED_POINTS,29 CERT_POINTS,29 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 3 as ppid,'OFL' as catgcode, 223 as earned_points,0 BORROWED_POINTS,223 CERT_POINTS,23 DISCARD_POINTS,200 used from dual
union
select 4 as ppid,'ONL' as catgcode, 109 as earned_points,0 BORROWED_POINTS,109 CERT_POINTS,9 DISCARD_POINTS,100 used from dual
union
select 4 as ppid,'OFL' as catgcode, 169 as earned_points,0 BORROWED_POINTS,169 CERT_POINTS,69 DISCARD_POINTS,100 used from dual
tbl_link as
select 1 as ppid,1 as pid, 'B' as code,'ONL' as catgcode, 53 as earned_points from dual
union
select 1 as ppid,12 as pid, 'W' as code,'OFL' as catgcode, 26 as earned_points from dual
union
select 1 as ppid,13 as pid, 'G' as code,'OFL' as catgcode, 87 as earned_points from dual
union
select 1 as ppid,14 as pid, 'P' as code,'OFL' as catgcode, 110 as earned_points from dual
union
select 2 as ppid,2 as pid, 'B' as code,'ONL' as catgcode, 39 as earned_points from dual
union
select 2 as ppid,22 as pid, 'W' ,'OFL' as catgcode, 30 as earned_points from dual
union
select 2 as ppid,23 as pid, 'G' ,'OFL' as catgcode, 29 as earned_points from dual
union
select 2 as ppid,24 as pid, 'P' ,'OFL' as catgcode, 31 as earned_points from dual
union
select 3 as ppid,3 as pid, 'B' as code,'ONL' as tier_catgcode, 109 as earned_points from dual
union
select 3 as ppid,32 as pid, 'W' ,'OFL' , 26 as earned_points from dual
union
select 3 as ppid,33 as pid, 'G' ,'OFL', 87 as earned_points from dual
union
select 3 as ppid,34 as pid, 'P' ,'OFL' , 110 as earned_points from dual
union
select 4 as ppid,4 as pid, 'B' as code,'ONL' as catgcode, 109 as earned_points from dual
union
select 4 as ppid,42 as pid, 'W' as code,'OFL' , 26 as earned_points from dual
union
select 4 as ppid,43 as pid, 'G' as code,'OFL' , 87 as earned_points from dual
union
select 4 as ppid,44 as pid, 'P' as code,'OFL' , 56 as earned_points from dual
final (PARENT_PROFILE_ID,PROFILE_ID,catgcode,EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED)
as (
select A.PPID PARENT_PROFILE_ID,B.PID PROFILE_ID,A.catgcode,B.EARNED_POINTS,BORROWED_POINTS,CERT_POINTS,DISCARD_POINTS,USED
from tbl_SUMMARY a,tbl_link b where a.ppid=b.ppid AND A.catgcode=B.catgcode
ORDER BY PROFILE_ID
select * from final order by 1;
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED
1 1 ONL 53 47 100 0 100
1 14 OFL 110 0 176 76 100
1 13 OFL 87 0 176 76 100
1 12 OFL 26 0 176 76 100
2 2 ONL 39 61 100 0 100
2 24 OFL 31 0 29 29 100
2 23 OFL 29 0 29 29 100
2 22 OFL 30 0 29 29 100
3 32 OFL 26 0 223 23 200
3 33 OFL 87 0 223 23 200
3 34 OFL 110 0 223 23 200
3 3 ONL 109 0 109 9 100
4 42 OFL 26 0 169 69 100
4 43 OFL 87 0 169 69 100
4 44 OFL 56 0 169 69 100
4 4 ONL 109 0 109 9 100
Need Output as below :
For parent profile 1, whatever i mentioned above is not correct. Borrowed 47 points from OFL to ONL to make ONL and also from OFL category has 176 points remaining after lending to ONL and using only 100 points, remaining 76 points are discarded. Need to deduct these 76 points also from child profiles. Output will be as below.
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
1 1 ONL 53 47 100 0 100 -53 0
1 12 OFL 26 0 176 76 100 -26 0
1 13 OFL 87 0 176 76 100 -74 -13
1 14 OFL 110 0 176 76 100 -47 -63
For parent profile id 2 --> ONL category has 39 points, so borrowed 61 points from OFL category to make ONL points 100.
Now need to populate tbl_link table at child profile level (i.e. child profiles 22,23,24).
Borrowed 61 points from OFL and need to deduct this points from the profile which has highest earned points, in this case deduct from profile 24 which has 31 points, from profile 22 which has 30 points. Need output like below
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
2 2 ONL 39 61 100 0 100 -39 0
2 22 OFL 30 0 29 29 100 -30 0
2 23 OFL 29 0 29 29 100 0 -29
2 24 OFL 31 0 29 29 100 -31 0
For parent profile id 3 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 32,33,34).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 223 points total. need only 200 points (i.e. mutiple of 100) for our process, 23 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 34. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
3 3 ONL 109 0 109 9 100 -100 -9
3 32 OFL 26 0 223 23 200 -26 0
3 33 OFL 87 0 223 23 200 -87 0
3 34 OFL 110 0 223 23 200 -87 -23
For parent profile id 4 --> ONL category has 109 points, so no need to borrow points from OFL category
Now need to populate tbl_link table at child profile level (i.e. child profiles 42,43,44).
in this case ONL has 100 points, so move the remaining 9 points will be expired. OFL category has 169 points total. need only 100 points (i.e. mutiple of 100) for our process, 69 points will be expired and has to deduct from the profile which has highest earned points, in this case from profile 43. Output :
PARENT_PROFILE_ID PROFILE_ID CATGCODE EARNED_POINTS BORROWED_POINTS CERT_POINTS DISCARD_POINTS USED BURN_PTS EXPIRE_PTS
4 4 ONL 109 0 109 9 100 100 9
4 42 OFL 26 0 169 69 100 -26 0
4 43 OFL 87 0 169 69 100 -18 -69
4 44 OFL 56 0 169 69 100 -56 0
Can someone help with the query. I googled about looping in sql and came to know that Oracle has a feature MODEL to loop in SQL, but i don't have idea on using MODEL clause.
Appreciate your help!
Thanks
Sri -
Cell assignments in Model clause
Hi,
I have a table TEST_DATA with YEAR,FIELD,VALUE as columns.
The VALUE1 , VALUE2, VALUE 3 columns are determined based on the field values. If the data is as follows
YEAR FIELD VALUE
2010 1 11111
2010 2 22222
2010 3 33333
Can I get the output as follows using model clause.
YEAR VALUE1 VALUE2 VALUE3
2010 11111 222222 333333
2009 23243 533635 343434
2008 34343 364653 343434
2007 34345 789879 343434
I don't want to use pivot as I have to perform some computations on the above value columns. So I tried the following query using model clause
and value1,value2,value3 are coming in 3 different rows.
How do I write cell assignments to get the value 1,value 2,value 3 in the same row?
SELECT YEAR, VALUE 1,VALUE 2, VALUE 3
FROM TEST_FIN_DATA
MODEL
RETURN UPDATED ROWS
MAIN simple
PARTITION BY (YEAR)
DIMENSION BY (FIELD)
MEASURES (VALUE VALUE1, VALUE VALUE2, VALUE VALUE3 )
RULES
(value1['1'] = value1['1'] ,
value[('2'] = value['2'],
value['3'] = value['3']);with data as (
select 2010 y, 1 f, 11111 v from dual union all
select 2010, 2, 222222 from dual union all
select 2010, 3, 333333 from dual union all
select 2009, 1, 111111 from dual union all
select 2009, 2, 222222 from dual union all
select 2009, 3, 333333 from dual
select y, v1, v2, v3 from
data
model
return updated rows
partition by (y)
dimension by (f)
measures (v, 0 v1, 0 v2, 0 v3)
rules(
v1[0]=v[1]
,v2[0]=v[2]
,v3[0]=v[3]
order by y desc
Y;V1;V2;V3
2010;11111;222222;333333
2009;111111;222222;333333compare to the query without return updated rows.
Trick is v?[0] creates a new (PIVOT-)row for every partition.
regards
Edited by: chris227 on 27.06.2012 06:15 -
Bin fitting with a counter using both analytics and model clause.
11.2.0.3
This falls under 'just want to figure out how to do it'. Its not critical for work. I want to try to see if its possible to do this with both analytic function and with a model clause. Just to see if its possible. It has been stumping me.
I got the idea to look at this from this article about bin fitting. I have been playing with the model clause and I think you would do something with the 'increment' clause, but I dont see a way to reset it to 1 at a certain point.
http://www.oracle.com/technetwork/issue-archive/2012/12-mar/o22asktom-1518271.html
The case I want to look at is, bin fitting based on a counter and a partition by. In theory this should be simpler than the example in the link.
[code]
create table myrooms (
room_number number,
person_id number);
create unique index myrooms_ind on myrooms(room_number,person_id);
[/code]
Person_id is not unique. So row_number is more appropriate than rank or dense_rank.
Problem: Partition by room_number, assign up to 50 people to a specific group with in the same room. This seems like it could be handled with a row_number() and a window clause, but that is not supported.
I need to basically translate the old procedural counter into sql:
pseudo-code that does not compile that would have a reason to use this logic.
[code]
declare
cursor curGetRoom
select room_number,person_id
from my rooms
order by room_number;
counter number := 1;
vCurrentRoom myroom.room_number%type;
begin
for i in curGetRoom loop
if vCurrentRoom is null then
vCurrentRoom := i.room_number;
elsif vCurrentRoom = i.room_number then
if counter < 51 then counter :=counter+1;
else counter := 1;
else
vCurrentRoom := i.room_number;
counter :=1;
end if;
end;
[/code]
simple partition query., but I dont see a way to limit this to 50 and then start over. Window functions are not supported with row_number()
[code]
select room_number,person_id,row_number() over (partition by room_number order by person_id) rn
from myrooms
[/code]11.2.0.3
This falls under 'just want to figure out how to do it'. Its not critical for work. I want to try to see if its possible to do this with both analytic function and with a model clause. Just to see if its possible. It has been stumping me.
I got the idea to look at this from this article about bin fitting. I have been playing with the model clause and I think you would do something with the 'increment' clause, but I dont see a way to reset it to 1 at a certain point.
http://www.oracle.com/technetwork/issue-archive/2012/12-mar/o22asktom-1518271.html
The case I want to look at is, bin fitting based on a counter and a partition by. In theory this should be simpler than the example in the link.
[code]
create table myrooms (
room_number number,
person_id number);
create unique index myrooms_ind on myrooms(room_number,person_id);
[/code]
Person_id is not unique. So row_number is more appropriate than rank or dense_rank.
Problem: Partition by room_number, assign up to 50 people to a specific group with in the same room. This seems like it could be handled with a row_number() and a window clause, but that is not supported.
I need to basically translate the old procedural counter into sql:
pseudo-code that does not compile that would have a reason to use this logic.
[code]
declare
cursor curGetRoom
select room_number,person_id
from my rooms
order by room_number;
counter number := 1;
vCurrentRoom myroom.room_number%type;
begin
for i in curGetRoom loop
if vCurrentRoom is null then
vCurrentRoom := i.room_number;
elsif vCurrentRoom = i.room_number then
if counter < 51 then counter :=counter+1;
else counter := 1;
else
vCurrentRoom := i.room_number;
counter :=1;
end if;
end;
[/code]
simple partition query., but I dont see a way to limit this to 50 and then start over. Window functions are not supported with row_number()
[code]
select room_number,person_id,row_number() over (partition by room_number order by person_id) rn
from myrooms
[/code] -
Using the Case clause with Model clause
Hello PL SQL gurus
I've used some scripts I've found on these forums to create a mortgage amortization statement. What I am trying to accomplish is getting the script to run a calculation or use a value within a table based upon the value in that table.
Here are the two tables:
CREATE TABLE mortgage_facts (customer VARCHAR2(20), fact VARCHAR2(20),
amount NUMBER(10,3));
INSERT INTO mortgage_facts VALUES ('Smith', 'Loan', 131828.81);
INSERT INTO mortgage_facts VALUES ('Smith', 'Annual_Interest', 3.348);
INSERT INTO mortgage_facts VALUES ('Smith', 'Payments', 72);
INSERT INTO mortgage_facts VALUES ('Smith', 'PaymentAmt', 0);
CREATE TABLE mortgage (customer VARCHAR2(20), pmt_num NUMBER(4), principalp NUMBER(10,3), interestp NUMBER(10,3), mort_balance NUMBER(10,3));
INSERT INTO mortgage VALUES ('Smith',0, 0, 0, 131828.81);
If the value within the table mortgage_facts is zero, I want the script to run a calculation to be used in a MODEL clause. If it is not zero, I would like to use that value instead of the calculation. Below is the script that I am getting an error on (I have bolded the portion in question):
SELECT c, p, to_char(round(m,2),'fm$9999999.00') principal_balance,
to_char(round(pp,2),'fm$9999999.00') towards_principal,
to_char(round(ip,2),'fm$9999999.00') towards_interest,
to_char(round(mp,2),'fm$9999999.00') monthly_payment
FROM MORTGAGE
MODEL --See 1
IGNORE NAV
REFERENCE R ON
*(SELECT customer, fact, amt --See 2*
FROM mortgage_facts
*MODEL DIMENSION BY (customer, fact) MEASURES (amount amt) --See 3*
RULES SEQUENTIAL ORDER
CASE WHEN mortgage_facts.fact = 'PaymentAmt' AND mortage_facts.amt = 0 THEN
*amt[ANY, 'PaymentAmt'] = mortgage_facts.amt*
ELSE
*amt[any, 'PaymentAmt']= (amt[CV(),'Loan']**
*Power(1+ (amt[CV(),'Annual_Interest']/100/12),*
*amt[CV(),'Payments']) **
*(amt[CV(),'Annual_Interest']/100/12)) /*
*(Power(1+(amt[CV(),'Annual_Interest']/100/12),*
*amt[CV(),'Payments']) - 1)*
END
DIMENSION BY (customer cust, fact) measures (amt)
MAIN amortization
PARTITION BY (customer c)
DIMENSION BY (0 p)
MEASURES (principalp pp, interestp ip, mort_balance m, customer mc, 0 mp )
RULES SEQUENTIAL ORDER
ITERATE(1000) UNTIL (ITERATION_NUMBER+1 =
r.amt[mc[0],'Payments'])
(ip[ITERATION_NUMBER+1] = m[CV()-1] *
r.amt[mc[0], 'Annual_Interest']/1200,
mp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'],
pp[ITERATION_NUMBER+1] = r.amt[mc[0], 'PaymentAmt'] - ip[CV()],
m[ITERATION_NUMBER+1] = m[CV()-1] - pp[CV()]
ORDER BY c, p
Any help is much appreciated. Thank you!!Ok, here we go, one way with iterative model:
select *
from mortgage_facts
model
partition by (Customer)
dimension by (1 p)
measures(loan, payments, INTEREST, PAYMENTAMT, INTERESTPMT, PRINCIPALPMT, balance)
rules iterate(1e9) until (iteration_number+2 >= payments[1])
(loan[iteration_number+2]=loan[1]
,payments[iteration_number+2]=cv(p)-1
,interest[iteration_number+2]=interest[1]
,paymentamt[iteration_number+2]=ROUND(
(LOAN[1] * (INTEREST[1]/12/100)*Power((1+INTEREST[1]/12/100), PAYMENTS[1])/(Power((1+INTEREST[1]/12/100),PAYMENTS[1])-1)), 2)
,INTERESTPMT[iteration_number+2]=round(balance[cv(p)-1]*interest[1]/1200, 2)
,PRINCIPALPMT[iteration_number+2]=paymentamt[cv()]-INTERESTPMT[cv()]
,balance[iteration_number+2]=balance[cv()-1]-PRINCIPALPMT[cv()]
CUSTOMER P LOAN PAYMENTS INTEREST PAYMENTAMT INTERESTPMT PRINCIPALPMT BALANCE
Smith 1 131828.81 72 3.348 0 0 0 131828.81
Smith 2 131828.81 1 3.348 2023.55 367.8 1655.75 130173.06
Smith 3 131828.81 2 3.348 2023.55 363.18 1660.37 128512.69
Smith 4 131828.81 3 3.348 2023.55 358.55 1665 126847.69
Smith 5 131828.81 4 3.348 2023.55 353.91 1669.64 125178.05
Smith 6 131828.81 5 3.348 2023.55 349.25 1674.3 123503.75
Smith 7 131828.81 6 3.348 2023.55 344.58 1678.97 121824.78
Smith 8 131828.81 7 3.348 2023.55 339.89 1683.66 120141.12
Smith 9 131828.81 8 3.348 2023.55 335.19 1688.36 118452.76
Smith 10 131828.81 9 3.348 2023.55 330.48 1693.07 116759.69
Smith 11 131828.81 10 3.348 2023.55 325.76 1697.79 115061.9
Smith 12 131828.81 11 3.348 2023.55 321.02 1702.53 113359.37
Smith 13 131828.81 12 3.348 2023.55 316.27 1707.28 111652.09
Smith 14 131828.81 13 3.348 2023.55 311.51 1712.04 109940.05
.... -
MODEL clause using CONNECY BY PRIOR
Hello.
I have the following table, CONTEXT_MAPPING:
Name Null? Type
ID NOT NULL NUMBER(38)
CONTEXT_ITEM NOT NULL VARCHAR2(30)
ID_1 NUMBER(38)
ID_2 NUMBER(38)
ID_3 NUMBER(38)
ID_4 NUMBER(38)
ID_5 NUMBER(38)
It's a self referencing table, i.e. ID_1 will refer to an ID from another row in CONTEXT_MAPPING, and the same for ID_2/3/4/5.
Easily illustrated through the following hierarchical data:
ID CONTEXT_ITEM ID1 ID2 ID3 ID4 ID5
1 P_DMA_NDA_ID
2 P_DS_NDA_ID 1
3 P_AST_NDA_ID 2 1
4 P_AGI_ID 3
5 P_ASG_NDA_ID 3
6 P_NTS_FACTS 5 5
7 P_IDE_VALUE 2 1
8 P_EIT_VALUE 2 1
9 P_TRI_TABLE 4 6 7 8
10 P_TRI 9
11 P_PRICE1 6 10 6
12 P_PRICE2 6 10 6
What I want to do, is for any context item, to identify ALL of its dependencies throughout the tree.
For example:
P_PRICE2 has a link to P_NTS_FACTS (ID1 & ID3 = 6) and P_TRI (ID2 = 10)
P_NTS_FACTS has a link to P_ASG_NDA_ID (ID1 & ID2 = 5), which in turn links to P_AST_NDA_ID (ID1 = 3)...
P_TRI has a link to P_TRI_TABLE (ID1 = 9), which in turn links to multiple context items (ID1 = 4, ID2 = 6 etc.) ....
....and so on, until we get to the "root" record, P_DMA_NDA_ID.
So, to see the complete dependency tree for P_PRICE2, I would expect to see a hierarchical result-set like this:
ID CONTEXT_ITEM ID1 ID2 ID3 ID4 ID5
12 P_PRICE2 6 10 6
10 P_TRI 9
9 P_TRI_TABLE 4 6 7 8
4 P_AGI_ID 3
6 P_NTS_FACTS 5 5
5 P_ASG_NDA_ID 3
3 P_AST_NDA_ID 2 1
7 P_IDE_VALUE 2 1
8 P_EIT_VALUE 2 1
2 P_DS_NDA_ID 1
1 P_DMA_NDA_ID
Ideally I want to do this in a single SQL statement - I've tried using CONNECT BY PRIOR in conjunction with LEVEL to do this, but it only performs a hierarchical join for a single child-parent relationship, and I need this to work for up to five children.
Was starting to wonder if I could use the MODEL clause in conjunction with CONNECT BY PRIOR to achieve this - does anyone have any idea whether this type of recursion is possible?
Thanks,
Rayit only performs a hierarchical join for a single child-parent relationshipBeg to differ.
Oracle Database 10g Release 10.2.0.2.0 - Production
SQL> CREATE TABLE context_mapping (
2 id NUMBER(38),
3 context_item VARCHAR2(30),
4 id_1 NUMBER(38),
5 id_2 NUMBER(38),
6 id_3 NUMBER(38),
7 id_4 NUMBER(38),
8 id_5 NUMBER(38));
Table created.
SQL> INSERT INTO context_mapping VALUES (1, 'P_DMA_NDA_ID', NULL, NULL, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (2, 'P_DS_NDA_ID', 1, NULL, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (3, 'P_AST_NDA_ID', 2, 1, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (4, 'P_AGI_ID', 3, NULL, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (5, 'P_ASG_NDA_ID', 3, NULL, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (6, 'P_NTS_FACTS', 5, 5, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (7, 'P_IDE_VALUE', 2, 1, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (8, 'P_EIT_VALUE', 2, 1, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (9, 'P_TRI_TABLE', 4, 6, 7, 8, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (10, 'P_TRI', 9, NULL, NULL, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (11, 'P_PRICE1', 6, 10, 6, NULL, NULL);
1 row created.
SQL> INSERT INTO context_mapping VALUES (12, 'P_PRICE2', 6, 10, 6, NULL, NULL);
1 row created.
SQL> SELECT DISTINCT id, context_item, id_1, id_2, id_3, id_4, id_5
2 FROM context_mapping
3 START WITH id = 12
4 CONNECT BY id IN (PRIOR id_1, PRIOR id_2, PRIOR id_3, PRIOR id_4, PRIOR id_5)
5 ORDER BY id DESC;
ID CONTEXT_ITEM ID_1 ID_2 ID_3 ID_4 ID_5
12 P_PRICE2 6 10 6
10 P_TRI 9
9 P_TRI_TABLE 4 6 7 8
8 P_EIT_VALUE 2 1
7 P_IDE_VALUE 2 1
6 P_NTS_FACTS 5 5
5 P_ASG_NDA_ID 3
4 P_AGI_ID 3
3 P_AST_NDA_ID 2 1
2 P_DS_NDA_ID 1
1 P_DMA_NDA_ID
11 rows selected.
SQL> -
Version
SQL> select *
2 from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - ProductionMy query
with tmp AS (
select 1 as num, 'karthik' as txt from dual UNION select 2 as num, 'john' as txt from dual UNION select 3 as num, '' as txt from dual UNION select 4 as num, '' as txt from dual UNION
select 14 as num, 'tom' as txt from dual UNION select 15 as num, '' as txt from dual UNION select 26 as num, 'sam' as txt from dual UNION
select 27 as num, '' as txt from dual UNION select 28 as num, '' as txt from dual
select *
from
select num,txt,rw,'G'||dense_rank() over(order by (num-rw)) grp_id
from
select
num, txt,row_number() over(order by num) rw
from tmp
model partition by(grp_id)
dimension by(num)
measures(txt,cast(null as varchar2(4000)) as last_row_col)
rules (last_row_col[(num)] = max(txt)[num < cv()])
GRP_ID NUM TXT LAST_ROW_COL
G1 1 karthik
G1 2 john karthik
G1 3 karthik
G1 4 karthik
G3 26 sam
G3 27 sam
G3 28 sam
G2 14 tom
G2 15 tomDesired Output :
GRP_ID NUM TXT LAST_ROW_COL
G1 1 karthik karthik
G1 2 john
G1 3
G1 4 john
G3 26 sam
G3 27
G3 28 sam
G2 14 tom
G2 15 tomi.e.within a group (GRP_ID) the column LAST_ROW_COL must hold the most recent(order by num desc) not null value to be displayed at the last row in that particular group.
So,it should be 'john' for the rest of null values in group G1(karthik will remain as it is for num = 1) which should be displayed at the ending row of that particular group.
Thanks in advance.
Edited by: RGH on Jan 2, 2012 4:18 AMRGH wrote:
My queryAnd why do you want to use MODEL for that? All you need is analytic functions:
with tmp AS (
select 1 as num, 'karthik' as txt from dual UNION ALL
select 2 as num, 'john' as txt from dual UNION ALL
select 3 as num, '' as txt from dual UNION ALL
select 4 as num, '' as txt from dual UNION ALL
select 14 as num, 'tom' as txt from dual UNION ALL
select 15 as num, '' as txt from dual UNION ALL
select 26 as num, 'sam' as txt from dual UNION ALL
select 27 as num, '' as txt from dual UNION ALL
select 28 as num, '' as txt from dual
select 'G' || dense_rank() over(order by num - rw) grp_id,
num,
txt,
last_row_col
from (
select num,
txt,
case
when lead(txt) over(order by num) is not null then last_value(txt ignore nulls) over(order by num)
when num = max(num) over() then last_value(txt ignore nulls) over(order by num)
end last_row_col,
row_number() over(order by num) rw
from tmp
GRP_ID NUM TXT LAST_RO
G1 1 karthik karthik
G1 2 john
G1 3
G1 4 john
G2 14 tom
G2 15 tom
G3 26 sam
G3 27
G3 28 sam
9 rows selected.
SQL> SY. -
I have the privilege of performing a very tedious task.
We have some home grown regular expressions in our company. I now need to expand these regular expressions.
Samples:
a = 0-3
b = Null, 0, 1
Expression: Meaning
1:5: 1,2,3,4,5
1a: 10, 11, 12, 13
1b: 1, 10, 11
1[2,3]ab: 120, 1200, 1201, ....
It get's even more inetersting because there is a possibility of 1[2,3]a.ab
I have created two base queries to aid me in my quest. I am using the SQL MODEL clause to solve this problem. I pretty confident that I should be able to convert evrything into a range and the use one of the MODEL clause listed below.
My only confusion is how do I INCREMENT dynamically. The INCREMENT seems to be a constant in both a FOR and ITERATE statement. I need to figure a way to increment with .01, .1, etc.
Any help will be greatly appreciated.
CODE:
Reference: http://www.sqlsnippets.com/en/topic-11663.html
Objective: Expand a range with ITERATE
WITH t AS
(SELECT '2:4' pt
FROM DUAL
UNION ALL
SELECT '6:9' pt
FROM DUAL)
SELECT pt AS code_expression
-- , KEY
-- , min_key
-- , max_key
, m_1 AS code
FROM t
MODEL
PARTITION BY (pt)
DIMENSION BY ( 0 AS KEY )
MEASURES (
0 AS m_1,
TO_NUMBER(SUBSTR(pt, 1, INSTR(pt, ':') - 1)) AS min_key,
TO_NUMBER(SUBSTR(pt, INSTR(pt, ':') + 1)) AS max_key
RULES
-- UPSERT
ITERATE (100000) UNTIL ( ITERATION_NUMBER = max_key[0] - min_key[0] )
m_1[ITERATION_NUMBER] = min_key[0] + ITERATION_NUMBER
ORDER BY pt, m_1
Explanation:
Line numbers are based on the assupmtion that "WITH t AS" starts at line 5.
If you need detailed information regarding the MODEL clause please refer to
the Refrence site stated above or read some documentation.
Partition-
Line 18: PARTITION BY (pt)
This will make sure that each "KEY" will start at 0 for each value of pt.
Dimension-
Line 19: DIMENSION BY ( 0 AS KEY )
This is necessary for the refrences max_key[0], and min_key[0] to work.
Measures-
Line 21: 0 AS m_1
A space holder for new values.
Line 22: TO_NUMBER(SUBSTR(pt, 1, INSTR(pt, ':') - 1)) AS min_key
The result is '1' for '1:5'.
Line 23: TO_NUMBER(SUBSTR(pt, INSTR(pt, ':') + 1)) AS max_key
The result is '5' for '1:5'.
Rules-
Line 26: UPSERT
This makes it possible for new rows to be created.
Line 27: ITERATE (100000) UNTIL ( ITERATION_NUMBER = max_key[0] - min_key[0] )
This reads ITERATE 100000 times or UNTIL the ITERATION_NUMBER = max_key[0] - min_key[0]
which would be 4 for '1:5', but since the ITERATION_NUMBER starts at 0, whatever follows
is repaeted 5 times.
Line 29: m_1[ITERATION_NUMBER] = min_key[0] + ITERATION_NUMBER
m_1[ITERATION_NUMBER] means m_1[Value of Dimension KEY].
Thus for each row of KEY the m_1 is min_key[0] + ITERATION_NUMBER.
Reference: http://www.sqlsnippets.com/en/topic-11663.html
Objective: Expand a range using FOR
WITH t AS
(SELECT '2:4' pt
FROM DUAL
UNION ALL
SELECT '6:9' pt
FROM DUAL)
, base AS
SELECT pt AS code_expression
, KEY AS code
, min_key
, max_key
, my_increment
, m_1
FROM t
MODEL
PARTITION BY (pt)
DIMENSION BY ( CAST(0 AS NUMBER) AS KEY )
MEASURES (
CAST(NULL AS CHAR) AS m_1,
TO_NUMBER(SUBSTR(pt, 1, INSTR(pt, ':') - 1)) AS min_key,
TO_NUMBER(SUBSTR(pt, INSTR(pt, ':') + 1)) AS max_key,
.1 AS my_increment
RULES
-- UPSERT
m_1[FOR KEY FROM min_key[0] TO max_key[0] INCREMENT 1] = 'Y'
ORDER BY pt, KEY, m_1
SELECT code_expression, code
FROM base
WHERE m_1 = 'Y'
Explanation:
Line numbers are based on the assupmtion that "WITH t AS" starts at line 5.
If you need detailed information regarding the MODEL clause please refer to
the Refrence site stated above or read some documentation.
Partition-
Line 21: PARTITION BY (pt)
This will make sure that each "KEY" will start at 0 for each value of pt.
Dimension-
Line 22: DIMENSION BY ( 0 AS KEY )
This is necessary for the refrences max_key[0], and min_key[0] to work.
Measures-
Line 24: CAST(NULL AS CHAR) AS m_1
A space holder for results.
Line 25: TO_NUMBER(SUBSTR(pt, 1, INSTR(pt, ':') - 1)) AS min_key
The result is '1' for '1:5'.
Line 26: TO_NUMBER(SUBSTR(pt, INSTR(pt, ':') + 1)) AS max_key
The result is '5' for '1:5'.
Line 27: .1 AS my_increment
The INCREMENT I would like to use.
Rules-
Line 30: UPSERT
This makes it possible for new rows to be created.
However seems like it is not necessary.
Line 32: m_1[FOR KEY FROM min_key[0] TO max_key[0] INCREMENT 1] = 'Y'
Where the KE value is between min_key[0] and max_key[0] set the value of m_1 to 'Y'
*/Of course, you can accomplish the same thing without MODEL using an Integer Series Generator like this.
create table t ( min_val number, max_val number, increment_size number );
insert into t values ( 2, 3, 0.1 );
insert into t values ( 1.02, 1.08, 0.02 );
commit;
create table integer_table as
select rownum - 1 as n from all_objects where rownum <= 100 ;
select
min_val ,
increment_size ,
min_val + (increment_size * n) as val
from t, integer_table
where
n between 0 and ((max_val - min_val)/increment_size)
order by 3
MIN_VAL INCREMENT_SIZE VAL
1.02 .02 1.02
1.02 .02 1.04
1.02 .02 1.06
1.02 .02 1.08
2 .1 2
2 .1 2.1
2 .1 2.2
2 .1 2.3
2 .1 2.4
2 .1 2.5
2 .1 2.6
2 .1 2.7
2 .1 2.8
2 .1 2.9
2 .1 3
15 rows selected.--
Joe Fuda
http://www.sqlsnippets.com/ -
Back-referencing data using MODEL
Not sure how possible this is...
with t as (select 1 as id, 'blah blah' as txt from dual union all
select 2, '# header 1' from dual union all
select 3, '41 text line 1' from dual union all
select 4, '22 text line 2' from dual union all
select 5, '17 text line 3' from dual union all
select 6, 'garbage' from dual union all
select 7, '# header 2' from dual union all
select 8, '77 text line 4' from dual union all
select 9, '80 text line 5' from dual union all
select 10, '#80 old text line 5' from dual union all
select 11, '14 text line 6' from dual union all
select 12, 'more garbage' from dual)In an ordered sequence of data, "Header" details need determining for all subsequent rows that begin with a <numeric> or #<numeric> value,
such that the output of the above data would appear as below:
ID TXT HEADER
1 blah blah
2 # header 1
3 41 text line 1 # header 1
4 22 text line 2 # header 1
5 17 text line 3 # header 1
6 garbage
7 # header 2
8 77 text line 4 # header 2
9 80 text line 5 # header 2
10 #80 old text line 5 # header 2
11 14 text line 6 # header 2
12 more garbageIs there a way to do this purely in SQL, with the model clause, perhaps using iterations?
select id, txt, header
from t
model dimension by (id)
measures (txt txt, CAST(NULL AS VARCHAR2(30)) header)
rules
header[id is any] = ?
)I'm still trying to get my head around the usage of the model clause, especially trying to reference backwards with iterations and the until clause.
Cheers
Blu
edit: just in case it's not obvious... "header" details are lines beginning with #<non numeric>
Message was edited by:
BluShadowJust for fun, a model clause solution:
SQL> with t as (select 1 as id, 'blah blah' as txt from dual union all
2 select 2, '# header 1' from dual union all
3 select 3, '41 text line 1' from dual union all
4 select 4, '22 text line 2' from dual union all
5 select 5, '17 text line 3' from dual union all
6 select 6, 'garbage' from dual union all
7 select 7, '# header 2' from dual union all
8 select 8, '77 text line 4' from dual union all
9 select 9, '80 text line 5' from dual union all
10 select 10, '#80 old text line 5' from dual union all
11 select 11, '14 text line 6' from dual union all
12 select 12, 'more garbage' from dual)
13 -- end of test data
14 select id
15 , txt
16 , header
17 from t
18 model
19 dimension by (id)
20 measures (txt, cast(null as varchar2(30)) header, cast(null as varchar2(30)) tmp)
21 rules
22 ( tmp[any] order by id
23 = case
24 when txt[cv()] like '# header%' then txt[cv()]
25 else tmp[cv()-1]
26 end
27 , header[any] order by id
28 = case
29 when txt[cv()] not like '# header%'
30 and translate(ltrim(substr(txt[cv()],1,instr(txt[cv()]||' ',' ')-1),'#'),'_0123456789','_') is null
31 then tmp[cv()]
32 end
33 )
34 /
ID TXT HEADER
1 blah blah
2 # header 1
3 41 text line 1 # header 1
4 22 text line 2 # header 1
5 17 text line 3 # header 1
6 garbage
7 # header 2
8 77 text line 4 # header 2
9 80 text line 5 # header 2
10 #80 old text line 5 # header 2
11 14 text line 6 # header 2
12 more garbage
12 rijen zijn geselecteerd.Regards,
Rob.
Maybe you are looking for
-
SMSs not notified nor shown in SMS folder
Hi! Q10 is my first BB and this issue is driving me crazy. When I receive a new SMS I don't get any notifications (popup, led or sound). Furthermore new SMSs are shown in Hub but not in SMS folder. I've tried soft and hard reset (i.e. removing batter
-
NetNanny MacBook Pro Internet problem.
From about the time that I got NetNanny on my Pro, it has been acting very strange. It says I'm connected to the internet, when I actually am not, and I can't get on anything that involves internet/wifi. All help would be great, if my loss of interne
-
Adobe Reader X embedded files in Power Point Presentation
I receive power point presentations with embedded pdf files. After installing Reader X these files no longer open. I see that other people have this problem but i have yet to see anyone say they have solved the problem. I have switched off the protec
-
[CS5 - JS] Random questions...
Hi. I come form InDesign scripting and I'm sorry to say that illustrator one is really raw... Anyway, my problems are: - Looks like it's not possible to get something like "textStyleRanges" within a paragraph so the only way to get different text sty
-
How do I Un-snooze a Reminder?
Hey gang- I have a daily repeating Reminder on my iPhone that displays an Alert every day at 4pm. Last week I tapped Snooze on the Reminder and as expected it repeated at 4:15. The problem is that now it displays every day at 4:15. The weird th