Home » SQL & PL/SQL » SQL & PL/SQL » Inserting records in Table using cursor (Oracle 11g)
Inserting records in Table using cursor [message #637071] Sun, 10 May 2015 00:46 Go to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi Guys,

I need to insert records from one table_1 to master_table by referencing in another ref_table which falls under condition. for e.g..

Table_1
col_1   col_2   col_3   col_4
12	123	200	11-Jun-14
12	123	3000	12-Jun-14
12	123	100	13-Jun-14


Ref_table
Start date	End date	Min	Max	Percentage Type
10-Jul-2001	15-Jun-2014	0	250	14.90%	   T1
10-Jul-2001	15-Jun-2014	250	2500	17% T2
10-Jul-2001	15-Jun-2014	2500	3500	29.90%	   T3


I need to insert into Master_table like this for e.g. for second record in table_1 where col_3 is 3000 that record col_4 falling between start date and end date of Ref_table and split the value like below

Master_table
Col_1 col_2 col_4     T1	T2   T3	        T1 %	T2%	T3 %
12    123   12-Jun-14 250	2250 500	14.90%	17%	29.90%



create table master_table(col_1 number,col_2 number,col_4 date,T1 number,t2 number,t3 number,t1_per number,t2_per number,t3_per number);
create table Ref_table(start_date date,end_date date,min number,max number,percentage number,type varchar2(5));
create table table_1(col_1 number,col_2 number,col_3 number,col_4 date);

insert into ref_table
select '10-Jul-2001','15-Jun-2014',0,250,14.9,T1 from dual;

insert into ref_table
select '10-Jul-2001','15-Jun-2014',250,2500,17,T2 from dual;

insert into ref_table
select '10-Jul-2001','15-Jun-2014',2500,3500,29.9,T3 from dual;

insert into Table_1
select 12,123,200,'11-Jun-2014' from dual;
insert into Table_1
select 12,123,3000,'12-Jun-2014' from dual;
insert into Table_1
select 12,123,100,'13-Jun-2014' from dual;




[Edit MC: add code tags and align columns]

[Updated on: Sun, 10 May 2015 00:59] by Moderator

Report message to a moderator

Re: Inserting records in Table using cursor [message #637073 is a reply to message #637071] Sun, 10 May 2015 01:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Use code tags for your result too.
Post a working test case.
For date column use TO_DATE with format as we have not the same default format than you:
SQL> select to_date('10-Jul-2001') from dual;
select to_date('10-Jul-2001') from dual
               *
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected


SQL> insert into ref_table
  2  select '10-Jul-2001','15-Jun-2014',0,250,14.9,T1 from dual;
select '10-Jul-2001','15-Jun-2014',0,250,14.9,T1 from dual
                                              *
ERROR at line 2:
ORA-00904: "T1": invalid identifier


There is no col3 colun in your result but there is one in your master_table; what should be its value.
Explain the value in each column of your result.

Re: Inserting records in Table using cursor [message #637074 is a reply to message #637073] Sun, 10 May 2015 01:11 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
What i exactly require is consider the record in table_1

col_1   col_2   col_3   col_4
12	123	    3000	12-Jun-14


I need to insert the above record into master_table by transforming col_3 value by looking in ref_table whether col_4 is between start date and end date and min and max and split the value accordingly in master_table

here 3000 - 250+2250 remaning 500(whatever) not required to fall between 2500-3500 in T3, but 0-250 and 250-2500 needs to be considered for splitting the values

Ref_table
Start date	          End date	Min	Max	 Percentage    Type
10-Jul-2001	15-Jun-2014	0	250	    14.90%	   T1
10-Jul-2001	15-Jun-2014	250	2500	17%            T2
10-Jul-2001	15-Jun-2014	2500 3500	29.90%	   T3


Col_1 col_2  col_4      T1	  T2      T3        T1 %	T2%	 T3 %
12    123   12-Jun-14 250	 2250 500	       14.90%	17%	 29.90%

[Updated on: Sun, 10 May 2015 01:12]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637075 is a reply to message #637074] Sun, 10 May 2015 01:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You didn't explain T1 T2 T3, still no col_3...
Still no valid test case.
Still no aligned columns.
Avoid tabulation in your result, we have not the same settings, use only spaces.

Re: Inserting records in Table using cursor [message #637076 is a reply to message #637075] Sun, 10 May 2015 01:55 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi,

I need col_3 from table_1 and compare with lookup referance table split the value accordingly and transform into master table while inserting, there is no col3 in master table because this needs to transformed like how i showed in master table result for one record from table_1
.
for eg 3000 needs to be split into
250(0-250) - T1 (check between Min and Max of ref_table)
2250(250-2500) - T2(check between Min and Max of ref table)
500(2500-3500)(remining not required to check between for this case - T3)

Table_1
col_1 col_2 col_3 col_4
12 123 3000 12-Jun-14

Ref_table
Start date End date Min Max Percentage Type
10-Jul-2001 15-Jun-2014 0 250 14.90% T1
10-Jul-2001 15-Jun-2014 250 2500 17% T2
10-Jul-2001 15-Jun-2014 2500 3500 29.90% T3


Master_table

Col_1 col_2 col_4 T1 T2 T3 T1 % T2% T3 %
12 123 12-Jun-14 250 2250 500 14.90% 17% 29.90%
Re: Inserting records in Table using cursor [message #637077 is a reply to message #637076] Sun, 10 May 2015 02:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I give up.

Re: Inserting records in Table using cursor [message #637078 is a reply to message #637077] Sun, 10 May 2015 02:33 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
HI Michel,

Can you let me know what you are not getting so that i can explain you further
Razz
Re: Inserting records in Table using cursor [message #637079 is a reply to message #637078] Sun, 10 May 2015 03:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You did not post what I asked in the way I asked.
And I have no test case.

[Updated on: Sun, 10 May 2015 03:33]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637081 is a reply to message #637079] Sun, 10 May 2015 04:01 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
HI michel,

Please find my queries

create table master_table(col_1 number,col_2 number,col_4 date,T1 number,t2 number,t3 number,t1_per number,t2_per number,t3_per number);
create table Ref_table(start_date date,end_date date,min number,max number,percentage number,type varchar2(5));
create table table_1(col_1 number,col_2 number,col_3 number,col_4 date);

insert into ref_table
select to_date('10-Jul-2001'),to_date('15-Jun-2014'),0,250,14.9,'T1' from dual;

insert into ref_table
select to_date('10-Jul-2001'),to_date('15-Jun-2014'),250,2500,17,'T2' from dual;

insert into ref_table
select to_date('10-Jul-2001'),to_date('15-Jun-2014'),2500,3500,29.9,'T3' from dual;

insert into Table_1
select 12,123,200,to_date('11-Jun-2014') from dual;
insert into Table_1
select 12,123,3000,to_Date('12-Jun-2014') from dual;
insert into Table_1
select 12,123,100,to_date('13-Jun-2014') from dual;

SQL> select * from table_1
  2  ;

     COL_1      COL_2      COL_3 COL_4
---------- ---------- ---------- ---------
        12        123        200 11-JUN-14
        12        123       3000 12-JUN-14
        12        123        100 13-JUN-14

SQL> select * from ref_table;

START_DAT END_DATE         MIN        MAX PERCENTAGE TYPE
--------- --------- ---------- ---------- ---------- -----
10-JUL-01 15-JUN-14          0        250       14.9 T1
10-JUL-01 15-JUN-14        250       2500         17 T2
10-JUL-01 15-JUN-14       2500       3500       29.9 T3

SQL>



i need the output to be inserted into master table from table_1 by looking up into ref_table based on below derivation logic

for eg 3000 needs to be split into
250(0-250) - T1 (check between Min and Max of ref_table)
2250(250-2500) - T2(check between Min and Max of ref table)
500(2500-3500)(remining not required to check between for this case - T3)


insert into master_table(col_1,col_2,col_4,t1,t2,t3,t1_per,t2_per,t3_per)
select col_1,
col2,
col_4,
250, -- derivation logic
2250, -- derivation logic
500,-- derivation logic
14.9,-- derivation logic
17,-- derivation logic
29.9-- derivation logic
from 
table_1;

master table
Col_1 col_2  col_4      T1	  T2      T3        T1 %	T2%	 T3 %
12    123   12-Jun-14 250	 2250 500	       14.90%	17%	 29.90%
Re: Inserting records in Table using cursor [message #637085 is a reply to message #637081] Sun, 10 May 2015 04:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You didn't read what I said.
SQL> insert into ref_table
  2  select to_date('10-Jul-2001'),to_date('15-Jun-2014'),0,250,14.9,'T1' from dual;
select to_date('10-Jul-2001'),to_date('15-Jun-2014'),0,250,14.9,'T1' from dual
               *
ERROR at line 2:
ORA-01858: a non-numeric character was found where a numeric was expected

Re: Inserting records in Table using cursor [message #637087 is a reply to message #637071] Sun, 10 May 2015 05:55 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi Michel,

I can able to insert thats what i have shown in output


create table master_table(col_1 number,col_2 number,col_4 date,T1 number,t2 number,t3 number,t1_per number,t2_per number,t3_per number);
create table Ref_table(start_date date,end_date date,min number,max number,percentage number,type varchar2(5));
create table table_1(col_1 number,col_2 number,col_3 number,col_4 date);



insert into ref_table
select to_date('10-Jul-2001'),to_date('15-Jun-2014'),0,250,14.9,'T1' from dual;

[Updated on: Sun, 10 May 2015 05:56]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637088 is a reply to message #637087] Sun, 10 May 2015 06:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I cannot, so it is wrong.
Read again my previous posts.

Re: Inserting records in Table using cursor [message #637091 is a reply to message #637088] Sun, 10 May 2015 06:15 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hope now you can use the below cases

insert into ref_table
select to_date('10-Jul-2001','DD-MON-YYYY'),to_date('15-Jun-2014','DD-MON-YYYY'),0,250,14.9,'T1' from dual;

insert into ref_table
select to_date('10-Jul-2001','DD-MON-YYYY'),to_date('15-Jun-2014','DD-MON-YYYY'),250,2500,17,'T2' from dual;

insert into ref_table
select to_date('10-Jul-2001','DD-MON-YYYY'),to_date('15-Jun-2014','DD-MON-YYYY'),2500,3500,29.9,'T3' from dual;

insert into Table_1
select 12,123,200,to_date('11-Jun-2014','DD-MON-YYYY') from dual;
insert into Table_1
select 12,123,3000,to_Date('12-Jun-2014','DD-MON-YYYY') from dual;
insert into Table_1
select 12,123,100,to_date('13-Jun-2014','DD-MON-YYYY') from dual


There is mistake from my master table output it should be like this

3000 = 250 (max for T1) + 2500 (max for T2) + 250 (remaining for T3)

master table
Col_1 col_2 col_4 T1 T2 T3 T1 % T2% T3 %
12 123 12-Jun-14 250 2500 250 14.90% 17% 29.90%

[Updated on: Sun, 10 May 2015 06:43]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637094 is a reply to message #637091] Sun, 10 May 2015 09:32 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi MIchel,
Hope now you are able to insert the query and work
Re: Inserting records in Table using cursor [message #637095 is a reply to message #637094] Sun, 10 May 2015 09:39 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It is a poor design that stores computed values in static table.
It is a poor design that stores same value in more than 1 static table.

Why not replace master table with a VIEW?
Re: Inserting records in Table using cursor [message #637096 is a reply to message #637095] Sun, 10 May 2015 10:14 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
How you are saying its storing same values, thats not its referring the satic table whether it matches the records and transforming the values, hope you understand my requirement, let me be clear once again
i need to take the data from table_1 and inserting into master_table by looking up from reference table whether the value falls between range

Table_1
col_1 col_2 col_3 col_4
12 123 3000 12-Jun-14

Ref_table
Start date End date Min Max Percentage Type
10-Jul-2001 15-Jun-2014 0 250 14.90% T1
10-Jul-2001 15-Jun-2014 250 2500 17% T2
10-Jul-2001 15-Jun-2014 2500 3500 29.90% T3


Master_table

Col_1 col_2 col_4 T1 T2 T3 T1 % T2% T3 %
12 123 12-Jun-14 250 2500 250 14.90% 17% 29.90%
Re: Inserting records in Table using cursor [message #637098 is a reply to message #637096] Sun, 10 May 2015 11:20 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Say MASTER_TABLE gets new values from your still missing SQL statement.
Later data in TABLE_1 or REF_TABLE gets updated. after this UPDATE now values in MASTER_TABLE are no longer correct.


Let us say you get the desired SQL to correctly load the desired results into MASTER_TABLE.
then new rows get populated into both TABLE_1 & REF_TABLE.
What happens when you run the same SQL again to load MASTER_TABLE?
Re: Inserting records in Table using cursor [message #637099 is a reply to message #637098] Sun, 10 May 2015 11:28 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
yeah but this will be delete and insert only and i agree with your point that if ref_table is getting changed in that case cursor can help in resolving instead of sql.thats why i require in cursor
Re: Inserting records in Table using cursor [message #637100 is a reply to message #637071] Sun, 10 May 2015 12:58 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
What's wrong with the replies you are receiving at OTN?

https://community.oracle.com/message/13059980#13059980
Re: Inserting records in Table using cursor [message #637101 is a reply to message #637100] Sun, 10 May 2015 13:06 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
don't think so any wrong rather i am looking for any solution approach as i am struck with the query
Re: Inserting records in Table using cursor [message #637102 is a reply to message #637099] Sun, 10 May 2015 13:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
rohit_shinez wrote on Sun, 10 May 2015 09:28
yeah but this will be delete and insert only and i agree with your point that if ref_table is getting changed in that case cursor can help in resolving instead of sql.thats why i require in cursor


It appears that you do not know what a cursor actually is.

https://docs.oracle.com/database/121/CNCPT/glossary.htm#CNCPT89131

cursor

A handle or name for a private SQL area in the PGA. Because cursors are closely associated with private SQL areas, the terms are sometimes used interchangeably.
Re: Inserting records in Table using cursor [message #637103 is a reply to message #637102] Sun, 10 May 2015 13:27 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
i Have tried something like this in sql but getting error

SELECT col_1, 
       col_2, 
       col_4, 
       t1, 
       t2, 
       t3, 
       t1per, 
       t2per, 
       t3per 
FROM   (SELECT col_1, 
               col_2, 
               col_4, 
               ( CASE 
                   WHEN col_3 > t1val THEN t1val 
                   ELSE col_3 
                 END ) T1, 
               ( CASE 
                   WHEN col_3 - t1val > 0 THEN col_3 - t1val 
                   ELSE 0 
                 END ) RESULT1, 
               ( CASE 
                   WHEN result1 > t2val THEN t2val 
                   ELSE result1 
                 END ) T2, 
               ( CASE 
                   WHEN result1 - t2val > 0 THEN result1 - t2val 
                   ELSE 0 
                 END ) T3, 
               t1per, 
               t2per, 
               t3per 
        FROM   (SELECT col_1, 
                       col_2, 
                       col_4, 
                       col_3, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T1' THEN REF.max 
                           END) T1VAL, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T2' THEN REF.max 
                           END) T2VAL, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T3' THEN REF.max 
                           END) T3VAL, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T1' THEN REF.percentage 
                           END) T1PER, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T2' THEN REF.percentage 
                           END) T2PER, 
                       Max(CASE 
                             WHEN ref.TYPE = 'T3' THEN REF.percentage 
                           END) T3PER 
                FROM   table_1 TB 
                       left outer join ref_table ref 
                                    ON TB.col_4 BETWEEN REF.start_date AND 
                                                        REF.end_date 
                GROUP  BY col_1, 
                          col_2, 
                          col_4) A)B 

stating that result1 is invalid identifier because result1 alias is not treating as alias

*BlackSwan formatted the SQL. Please do so yourself in the future.
http://www.orafaq.com/forum/t/174502/

[Updated on: Sun, 10 May 2015 13:42] by Moderator

Report message to a moderator

Re: Inserting records in Table using cursor [message #637105 is a reply to message #637103] Sun, 10 May 2015 14:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
when all else fails Read The Fine Manual

https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF01702
Re: Inserting records in Table using cursor [message #637107 is a reply to message #637103] Sun, 10 May 2015 16:05 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl> SELECT * FROM table_1
  2  /

     COL_1      COL_2      COL_3 COL_4
---------- ---------- ---------- -----------
        12        123        200 11-Jun-2014
        12        123       3000 12-Jun-2014
        12        123        100 13-Jun-2014

3 rows selected.

SCOTT@orcl> SELECT * FROM ref_table
  2  /

START_DATE  END_DATE           MIN        MAX PERCENTAGE TYPE
----------- ----------- ---------- ---------- ---------- -----
10-Jul-2001 15-Jun-2014          0        250       14.9 T1
10-Jul-2001 15-Jun-2014        250       2500         17 T2
10-Jul-2001 15-Jun-2014       2500       3500       29.9 T3

3 rows selected.

SCOTT@orcl> SELECT col_1, col_2, col_4,
  2  	    LEAST (t1val, col_3) t1,
  3  	    LEAST (t2val, GREATEST (col_3 - t1val, 0)) t2,
  4  	    LEAST (t3val, GREATEST (col_3 - t1val - t2val, 0)) t3,
  5  	    t1per, t2per, t3per
  6  FROM   (SELECT tb.col_1, tb.col_2, tb.col_4, tb.col_3,
  7  		    MAX (DECODE (ref.type, 'T1', ref.max)) t1val,
  8  		    MAX (DECODE (ref.type, 'T2', ref.max)) t2val,
  9  		    MAX (DECODE (ref.type, 'T3', ref.max)) t3val,
 10  		    MAX (DECODE (ref.type, 'T1', ref.percentage)) t1per,
 11  		    MAX (DECODE (ref.type, 'T2', ref.percentage)) t2per,
 12  		    MAX (DECODE (ref.type, 'T3', ref.percentage)) t3per
 13  	     FROM   table_1 tb
 14  		    LEFT OUTER JOIN ref_table ref
 15  		    ON tb.col_4 BETWEEN ref.start_date AND ref.end_date
 16  	     GROUP  BY tb.col_1, tb.col_2, tb.col_4, tb.col_3)
 17  ORDER  BY col_1, col_2, col_4
 18  /

     COL_1      COL_2 COL_4               T1         T2         T3      T1PER      T2PER      T3PER
---------- ---------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
        12        123 11-Jun-2014        200          0          0       14.9         17       29.9
        12        123 12-Jun-2014        250       2500        250       14.9         17       29.9
        12        123 13-Jun-2014        100          0          0       14.9         17       29.9

3 rows selected.

Re: Inserting records in Table using cursor [message #637131 is a reply to message #637071] Mon, 11 May 2015 07:28 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Thanks, with the same query is it possible to insert other columns of table_1 in master table since group by is used its difficult,
or to be precise i need to select other column from table_1

master_table

Col_1 col_2 col_4 col_5 T1 T2 T3 T1 % T2% T3 %
12 123 12-Jun-14  300 250 2500 250 14.90% 17% 29.90% 



[Updated on: Mon, 11 May 2015 07:38]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637136 is a reply to message #637131] Mon, 11 May 2015 08:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Is it possible you correctly format your post?

Re: Inserting records in Table using cursor [message #637143 is a reply to message #637136] Mon, 11 May 2015 13:09 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Actually this is just the enhancement of my previous requirement where i need to insert from master_table to monthly_table by referring another ref_table1

create ref_table1(col_1 number,col_2 number,start_date date, end_date date;
insert into ref_table1
select 12,123,to_date(12-Jun-2014,'DD-MON-YYYY'),to_date(13-Jun-2014,'DD-MON-YYYY') from dual;

insert into ref_table1
select 12,123,to_date(13-Jun-2014,'DD-MON-YYYY'),to_date(14-Jun-2014,'DD-MON-YYYY') from dual;

col_1,col_2,start_date, end_date
12,123,12-Jun-2014 , 13-Jun-2014
12,123,13-Jun-2014, 14-Jun-2014



[code

COL_1 COL_2 COL_4 T1 T2 T3 T1PER T2PER T3PER
---------- ---------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
12 123 11-Jun-2014 200 0 0 14.9 17 29.9
12 123 12-Jun-2014 250 2500 250 14.9 17 29.9
12 123 13-Jun-2014 100 0 0 14.9 17 29.9

][/code]

i need to transform the data into monthly_table by referring ref_table1 where col_4 is between start_date and end_date of ref_table1 and sum the values and count the number of respective types where not equal to zero.

T1_days - 2 T1 values i.e 250 and 100 hence it will be 2 days
T1_days - one T1 values not to consider 0, hence it will be 1 day


Monthly_table

col_1,col_2,start_date,end_date,T1,T2,T3,T1_days,T2_days,T3_days
12,123,12-jun-2014,13-jun-2014,350,2500,250,2,1,1



I tried like below

SELECT a.col_1, 
       a.col_2, 
       b.start_date, 
       b.end_date, 
       Sum(a.t1), 
       Sum(a.t2), 
       Sum(a.t3), 
       Count(*) 
FROM   master_table a, 
       ref_table1 b 
WHERE  a.col_4_date BETWEEN b.start_date AND b.end_date 
       AND a.col_1 = b.col_1 
       AND a.col_2 = b.col_2 

 

[Updated on: Mon, 11 May 2015 13:11]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637144 is a reply to message #637143] Mon, 11 May 2015 13:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Is it possible you correctly format your post?
Obviously it is not possible. Sad
Re: Inserting records in Table using cursor [message #637145 is a reply to message #637144] Mon, 11 May 2015 13:29 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I have formatted the SQL, i believe thats what i have read from the guide lines


create ref_table1(col_1 number,col_2 number,start_date date, end_date date;
insert into ref_table1
select 12,123,to_date(12-Jun-2014,'DD-MON-YYYY'),to_date(13-Jun-2014,'DD-MON-YYYY') from dual;

insert into ref_table1
select 12,123,to_date(13-Jun-2014,'DD-MON-YYYY'),to_date(14-Jun-2014,'DD-MON-YYYY') from dual;

ref_table1

col_1,col_2,start_date, end_date
12,123,12-Jun-2014 , 13-Jun-2014
12,123,13-Jun-2014, 14-Jun-2014

Master_table

COL_1 COL_2 COL_4 T1 T2 T3 T1PER T2PER T3PER
---------- ---------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
12 123 11-Jun-2014 200 0 0 14.9 17 29.9
12 123 12-Jun-2014 250 2500 250 14.9 17 29.9
12 123 13-Jun-2014 100 0 0 14.9 17 29.9




i need to transform the data into monthly_table by referring ref_table1 where col_4 is between start_date and end_date of ref_table1 and sum the values and count the number of respective types where not equal to zero.

T1_days - 2 T1 values i.e 250 and 100 hence it will be 2 days
T1_days - one T1 values not to consider 0, hence it will be 1 day

Monthly_table

col_1,col_2,start_date,end_date,T1,T2,T3,T1_days,T2_days,T3_days
12,123,12-jun-2014,13-jun-2014,350,2500,250,2,1,1





Query used

SELECT a.col_1, 
       a.col_2, 
       b.start_date, 
       b.end_date, 
       Sum(a.t1), 
       Sum(a.t2), 
       Sum(a.t3), 
       Count(*) 
FROM   master_table a, 
       ref_table1 b 
WHERE  a.col_4_date BETWEEN b.start_date AND b.end_date 
       AND a.col_1 = b.col_1 
       AND a.col_2 = b.col_2 


[Updated on: Mon, 11 May 2015 13:38]

Report message to a moderator

Re: Inserting records in Table using cursor [message #637146 is a reply to message #637143] Mon, 11 May 2015 13:33 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
rohit_shinez wrote on Mon, 11 May 2015 11:09

[code

COL_1 COL_2 COL_4 T1 T2 T3 T1PER T2PER T3PER
---------- ---------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
12 123 11-Jun-2014 200 0 0 14.9 17 29.9
12 123 12-Jun-2014 250 2500 250 14.9 17 29.9
12 123 13-Jun-2014 100 0 0 14.9 17 29.9

][/code]


does above look properly formatted to you?
Re: Inserting records in Table using cursor [message #637147 is a reply to message #637146] Mon, 11 May 2015 13:41 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
I have formatted the SQL, i believe thats what i have read from the guide lines

create ref_table1(col_1 number,col_2 number,start_date date, end_date date;
insert into ref_table1
select 12,123,to_date(12-Jun-2014,'DD-MON-YYYY'),to_date(13-Jun-2014,'DD-MON-YYYY') from dual;

insert into ref_table1
select 12,123,to_date(13-Jun-2014,'DD-MON-YYYY'),to_date(14-Jun-2014,'DD-MON-YYYY') from dual;

ref_table1

col_1,col_2,start_date, end_date
12,123,12-Jun-2014 , 13-Jun-2014
12,123,13-Jun-2014, 14-Jun-2014

Master_table

COL_1 COL_2 COL_4 T1 T2 T3 T1PER T2PER T3PER

12 123 11-Jun-2014 200 0 0 14.9 17 29.9
12 123 12-Jun-2014 250 2500 250 14.9 17 29.9
12 123 13-Jun-2014 100 0 0 14.9 17 29.9





i need to transform the data into monthly_table by referring ref_table1 where col_4 is between start_date and end_date of ref_table1 and sum the values and count the number of respective types where not equal to zero.

T1_days - 2 T1 values i.e 250 and 100 hence it will be 2 days
T1_days - one T1 values not to consider 0, hence it will be 1 day

Monthly_table

col_1,col_2,start_date,end_date,T1,T2,T3,T1_days,T2_days,T3_days
12,123,12-jun-2014,13-jun-2014,350,2500,250,2,1,1





Query used

SELECT a.col_1, 
       a.col_2, 
       b.start_date, 
       b.end_date, 
       Sum(a.t1), 
       Sum(a.t2), 
       Sum(a.t3), 
       Count(*) 
FROM   master_table a, 
       ref_table1 b 
WHERE  a.col_4_date BETWEEN b.start_date AND b.end_date 
       AND a.col_1 = b.col_1 
       AND a.col_2 = b.col_2 


Re: Inserting records in Table using cursor [message #637148 is a reply to message #637147] Mon, 11 May 2015 14:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Do you understand the term "align the columns"?

Re: Inserting records in Table using cursor [message #637153 is a reply to message #637147] Mon, 11 May 2015 19:50 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl> SELECT * FROM ref_table1 ORDER BY col_1, col_2, start_date
  2  /

     COL_1      COL_2 START_DATE  END_DATE
---------- ---------- ----------- -----------
        12        123 12-Jun-2014 13-Jun-2014
        12        123 13-Jun-2014 14-Jun-2014

2 rows selected.

SCOTT@orcl> SELECT * FROM master_table ORDER BY col_1, col_2, col_4
  2  /

     COL_1      COL_2 COL_4               T1         T2         T3     T1_PER     T2_PER     T3_PER
---------- ---------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
        12        123 11-Jun-2014        200          0          0       14.9         17       29.9
        12        123 12-Jun-2014        250       2500        250       14.9         17       29.9
        12        123 13-Jun-2014        100          0          0       14.9         17       29.9

3 rows selected.

SCOTT@orcl> SELECT a.col_1,
  2  	    a.col_2,
  3  	    b.start_date,
  4  	    b.end_date,
  5  	    SUM (a.t1) t1,
  6  	    SUM (a.t2) t2,
  7  	    SUM (a.t3) t3,
  8  	    SUM (DECODE (a.t1, 0, 0, 1)) t1_days,
  9  	    SUM (DECODE (a.t2, 0, 0, 1)) t2_days,
 10  	    SUM (DECODE (a.t3, 0, 0, 1)) t3_days
 11  FROM   master_table a,
 12  	    ref_table1 b
 13  WHERE  a.col_4 BETWEEN b.start_date AND b.end_date
 14  AND    a.col_1 = b.col_1
 15  AND    a.col_2 = b.col_2
 16  GROUP  BY a.col_1, a.col_2, b.start_date, b.end_date
 17  ORDER  BY col_1, col_2, start_date
 18  /

     COL_1      COL_2 START_DATE  END_DATE            T1         T2         T3    T1_DAYS    T2_DAYS    T3_DAYS
---------- ---------- ----------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
        12        123 12-Jun-2014 13-Jun-2014        350       2500        250          2          1          1
        12        123 13-Jun-2014 14-Jun-2014        100          0          0          1          0          0

2 rows selected.
Re: Inserting records in Table using cursor [message #637157 is a reply to message #637153] Mon, 11 May 2015 22:25 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
Hi, is it possible to change the value dynamically or i need to give update after insert query

for eg

so it would be dynamically

T1_avg - T1(after sum)/2(T1_days)
if divide by zero then i need to display as 0 to avoid divide by zero error

this can be achieved in same query or i need to update later this table

[cod
alter table monthly_table add t1_avg number;
alter table monthly_table add t2_avg number;
alter table monthly_table add t3_avg number;
alter table monthly_table add T1_sum_per number;
alter table monthly_table add T2_sum_per number;
alter table monthly_table add t3_sum_per number;
[/code]


COL_1      COL_2 START_DATE  END_DATE            T1         T2         T3    T1_DAYS    T2_DAYS    T3_DAYS ,T1_avg,T2,AVG,T3_AVG,T1_SUM_PER,T2_SUM_PER,T3_SUM_PER
---------- ---------- ----------- ----------- ---------- ---------- ---------- ---------- ---------- ----------
        12        123 12-Jun-2014 13-Jun-2014        350       2500        250          2          1          1 350/2,2500/1,250/1 , (14.9+14.9),34,31.9
        12        123 13-Jun-2014 14-Jun-2014        100          0          0          1          0          0,100/1,0,0,14.9,17,29.9


Re: Inserting records in Table using cursor [message #637158 is a reply to message #637157] Mon, 11 May 2015 22:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It is a poor design that stores computed values in static table.
Re: Inserting records in Table using cursor [message #637163 is a reply to message #637157] Tue, 12 May 2015 00:28 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You can do this in the same query by using one query as an inline view and calculating the additional columns in an outer query:

SELECT ..., t1/t1_days AS t1_avg, ...
FROM   (SELECT ...,
               ... t1,
               ...,
               ... t1_days
        FROM   ...)
/


As BlackSwan keep saying, it is better to do:

CREATE OR REPLACE VIEW master_table AS SELECT ...;

than

INSERT INTO master_table SELECT ...

as it is a bad practice to physically store calculated data when you only need to store the queries that calculate the data. You can select from these views just as you would from a table.
Re: Inserting records in Table using cursor [message #637165 is a reply to message #637163] Tue, 12 May 2015 02:32 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
In that case how its possible to handle divide by zero
Re: Inserting records in Table using cursor [message #637168 is a reply to message #637165] Tue, 12 May 2015 03:26 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Use a CASE statement
Re: Inserting records in Table using cursor [message #637369 is a reply to message #637098] Fri, 15 May 2015 13:07 Go to previous messageGo to next message
rohit_shinez
Messages: 139
Registered: January 2015
Senior Member
BlackSwan wrote on Sun, 10 May 2015 11:20
Say MASTER_TABLE gets new values from your still missing SQL statement.
Later data in TABLE_1 or REF_TABLE gets updated. after this UPDATE now values in MASTER_TABLE are no longer correct.


Let us say you get the desired SQL to correctly load the desired results into MASTER_TABLE.
then new rows get populated into both TABLE_1 & REF_TABLE.
What happens when you run the same SQL again to load MASTER_TABLE?



Can you explain how i can use this structure in a view
Re: Inserting records in Table using cursor [message #637371 is a reply to message #637369] Fri, 15 May 2015 13:31 Go to previous messageGo to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member

CREATE TABLE EMPLOYEE
(
    empl_id        NUMBER,
    empl_nm        VARCHAR2(50),
    monthly_sal    NUMBER(10,2),
    bonus          NUMBER(10,2),
);

CREATE OR replace VIEW vw_employee 
AS 
  SELECT empl_id, 
         empl_nm, 
         monthly_sal, 
         bonus, 
         ( monthly_sal * 12 + bonus ) total_sal 
  FROM   employee; 


or just include VIRTUAL column

http://viralpatel.net/blogs/oracle-11g-new-feature-virtual-column/
Previous Topic: Row level lock after updation
Next Topic: Format check on varchar check!!
Goto Forum:
  


Current Time: Wed Apr 24 20:43:20 CDT 2024