Home » SQL & PL/SQL » SQL & PL/SQL » update rows based on column values (oracle 10g)
update rows based on column values [message #610699] Sun, 23 March 2014 02:18 Go to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
i have a table called cut_sum where there is qty column and this qty is distributed into three different tables as single row per each qty and i am doing manual updates each time to the three tables everytime if there is any qty mismatch between summary table cut_sum and the other three detail tables as below.Is there a easier way to update.


        CREATE TABLE CUT_SUM ( CD_PM_CODE VARCHAR2(12) ,CD_PS_CODE VARCHAR2(12),CD_QTY NUMBER );
        
        INSERT INTO CUT_SUM (CD_PM_CODE ,CD_PS_CODE ,CD_QTY) VALUES ('A','A1001', 4) 
        
        INSERT INTO CUT_SUM (CD_PM_CODE ,CD_PS_CODE ,CD_QTY) VALUES ('A','A1002', 2) 
        
        INSERT INTO CUT_SUM (CD_PM_CODE ,CD_PS_CODE ,CD_QTY) VALUES ('B','B1001', 2)

--CD_QTY Is the sum of qty


--First table have qty more than cd_qty and hence one row needs to be updated as 0

CREATE TABLE CUT_SEC (SEC_PM_CODE VARCHAR2(12), SEC_PS_CODE VARCHAR2(12),SEC_QTY NUMBER);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);

--second table no need to update anything since the qty is matching.

      CREATE TABLE CUT_SEC_REF (SECR_PM_CODE VARCHAR2(12), SECR_PS_CODE VARCHAR2(12),SECR_QTY NUMBER);
        
        INSERT INTO CUT_SEC_REF ( SECR_PM_CODE,SECR_PS_CODE,SECR_QTY) VALUES ( 'A','A1002',1);
        
        INSERT INTO CUT_SEC_REF ( SECR_PM_CODE,SECR_PS_CODE,SECR_QTY) VALUES ( 'A','A1002',1);

--third table one row is missing hence one row has to be inserted with value as 1 qty .

        CREATE TABLE CUT_PL (PL_PM_CODE VARCHAR2(12), PL_PS_CODE VARCHAR2(12),PL_QTY NUMBER);      
        
        INSERT INTO CUT_PL (PL_PM_CODE ,PL_PS_CODE,PL_QTY) VALUES ('B','B1001',1)


--THE Problem is i can find out where the difference is using the view and goind and updating it manullay as below.

create or replace view cut_log as        
        SELECT sec_pm_code p ,sec_ps_code s,sum(sec_qty) qty FROM CUT_SEC
        group by sec_pm_code,sec_ps_code
        union
        select secr_pm_code p , secr_ps_code s , sum(secr_qty) qty 
        from cut_sec_ref
        group by secr_pm_code,secr_ps_code
        union
        select pl_pm_code p , pl_ps_code s , sum(pl_qty) qty
        from cut_pl
        group by pl_pm_code,pl_ps_code
        order by 1

select * from cut_sum,cut_log
        where p = cd_pm_code
        and s = cd_ps_code
        and cd_qty <> qty



Kindly advice how can i update these rows.






Re: update rows based on column values [message #610700 is a reply to message #610699] Sun, 23 March 2014 02:30 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Why do you replicate three times the same table with exactly the same structure? Have you checked the Normalization in your design?
Re: update rows based on column values [message #610701 is a reply to message #610700] Sun, 23 March 2014 02:50 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
yes i did, actually i am just providing the test case and my problem is to update the rows based on colum values.
Re: update rows based on column values [message #610702 is a reply to message #610701] Sun, 23 March 2014 02:53 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
m.abdulhaq wrote on Sun, 23 March 2014 08:50
yes i did, actually i am just providing the test case and my problem is to update the rows based on colum values.

You did what exactly in terms of normalization? explain clearly the purpose of each table along with the constraints.
Re: update rows based on column values [message #610703 is a reply to message #610702] Sun, 23 March 2014 03:39 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
Quote:

You did what exactly in terms of normalization? explain clearly the purpose of each table along with the constraints.


For normalization i am maintaining the referential keys to maintain the integrity among tables , like each of these tables will have a reference of master key from cut_sum,the clear purpose is the records will get generated in these three tables based on data of cut_sum for example if the qty is 3 pcs , each qty will be in each line or in other words each row will be generated per qty.

To be more specific, these three tables are for three different types of item.

Cut_sec - sec is a type of item where all relevant items of type sec will be stored.
Cut_Sec_ref - sec_ref another type of item
Cut_pl - Pl another type.

My program will read the cut_sum table and insert the quantity along with relevant type of item into relevant table.
If cut_Sec is having type sec type item with 3 pcs then 3 records will get inserted into cut_Sec table.

[Updated on: Sun, 23 March 2014 03:42]

Report message to a moderator

Re: update rows based on column values [message #610704 is a reply to message #610703] Sun, 23 March 2014 03:55 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
m.abdulhaq wrote on Sun, 23 March 2014 09:39
For normalization i am maintaining the referential keys to maintain the integrity among tables

Just because you have put some foreign key does not mean that your design is correct. You don't answer the question. Why defining three times the same table?

m.abdulhaq wrote on Sun, 23 March 2014 09:39
clear purpose is the records will get generated in these three tables based on data of cut_sum

That will not work in a multi-user environment with the manual update you're trying as different sessions will not see uncommitted data of others. Have a look at SELECT FOR UPDATE and FOR UPDATE Cursors in order to see what I'm talking about.

m.abdulhaq wrote on Sun, 23 March 2014 09:39
for example if the qty is 3 pcs , each qty will be in each line or in other words each row will be generated per qty.

It is possible to generate rows in SQL but again the original question should be answered why 3 tables? what's the point of generating these rows? Maybe there is some kind of view that you're looking for? The puropose/logic of your design is not clear (at least for me Smile ).

m.abdulhaq wrote on Sun, 23 March 2014 09:39
My program will read the cut_sum table and insert the quantity along with relevant type of item into relevant table. If cut_Sec is having type sec type item with 3 pcs then 3 records will get inserted into cut_Sec table.

Isn't this just the CD_PM_CODE column in the master table, given the fact that the quantity column (CD_QTY) is already there?

[Updated on: Sun, 23 March 2014 04:00]

Report message to a moderator

Re: update rows based on column values [message #610705 is a reply to message #610703] Sun, 23 March 2014 04:28 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
If the purpose is generating rows at run time based on the quantity of each item then you can use a Row Generator. Here is an example:

SQL> create table items(item_id varchar2(5) primary key, quantity number not null);

Table created.

SQL> insert into items(item_id, quantity) values('a', 3);

1 row created.

SQL> insert into items(item_id, quantity) values('b', 5);

1 row created.

SQL> insert into items(item_id, quantity) values('c', 2);

1 row created.

SQL> insert into items(item_id, quantity) values('d', 1);

1 row created.

SQL> create type numtab_ty as table of number
  2  /

Type created.

SQL> show errors;
No errors.
SQL> 
SQL> 
SQL> select
  2      t1.item_id
  3  from
  4      items t1
  5          cross join
  6      table
  7      (
  8          cast
  9          (
 10              multiset
 11              (
 12                  select
 13                      level
 14                  from
 15                      "PUBLIC".dual
 16                  connect by
 17                      level <= t1.quantity
 18              ) as numtab_ty
 19          )
 20      ) t2
 21  order by
 22    t1.item_id
SQL>



ITEM_
-----
a
a
a
b
b
b
b
b
c
c
d

11 rows selected.

SQL>
Re: update rows based on column values [message #610706 is a reply to message #610704] Sun, 23 March 2014 04:45 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
Quote:

It is possible to generate rows in SQL but again the original question should be answered why 3 tables? what's the point of generating these rows? Maybe there is some kind of view that you're looking for? The puropose/logic of your design is not clear (at least for me ).


thanks dariyoosh for the query , the point is we need to store this data into three different tables for the endorsements purpose , in our scope of buisness we need to maintain the history data of how many items are there in cut_sec,cut_sec_ref and cut_pl.Let me put one more small example.I came to this site hoping for some solution.

       CREATE TABLE CUT_SUM ( CD_PM_CODE VARCHAR2(12) ,CD_PS_CODE VARCHAR2(12),CD_QTY NUMBER );
        
        INSERT INTO CUT_SUM (CD_PM_CODE ,CD_PS_CODE ,CD_QTY) VALUES ('A','A1001', 4) 

--now i have work table or temporary table where the relevant records exists with higher quantity, now i want to reduce it to the above one.

CREATE TABLE CUT_SEC (SEC_PM_CODE VARCHAR2(12), SEC_PS_CODE VARCHAR2(12),SEC_QTY NUMBER);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);
        
        INSERT INTO CUT_SEC ( SEC_PM_CODE,SEC_PS_CODE,SEC_QTY) VALUES ( 'A','A1001',1);

--the data i want in cut_sec is

SEC_PM_CODE	SEC_PS_CODE	SEC_QTY
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	0



[EDITED by LF: removed superfluous empty lines]

[Updated on: Sun, 23 March 2014 08:34] by Moderator

Report message to a moderator

Re: update rows based on column values [message #610707 is a reply to message #610706] Sun, 23 March 2014 05:43 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Have a look at the sample data you provided
SEC_PM_CODE	SEC_PS_CODE	SEC_QTY
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	1

You're agree with me that there 5 exactly same rows in the table. Now according to you the above sample should become
SEC_PM_CODE	SEC_PS_CODE	SEC_QTY
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	1
A	           A1001	0

How do you know which row among the five above has to be updated by setting quantity to 0? Given the fact that rows are neither ordered in your table nor unique? Do you see the problem in the design?
Re: update rows based on column values [message #610708 is a reply to message #610707] Sun, 23 March 2014 06:00 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
thanks for the prompt response dariyoosh, yes you are right from the design point , but i need the last row to be 0 or in other words update the rows as 0 which are of greater than csd_qty , let it be any order.
Re: update rows based on column values [message #610710 is a reply to message #610708] Sun, 23 March 2014 06:22 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Sorry but I may not be able to help any more. I stressed a few points that IMHO are serious design problems and also I mentioned about the concurrency problem in a multi-user environment. So long as these (design) problems exist I don't see how a proper & simple pure SQL solution could be suggested (at least I don't have any).

For concurrency you may want to have a look at the link that I provided in my previous comments.

Therefore, I think I'm going to leave the topic to others who may be able to suggest a true/better solution (if there is any)
Re: update rows based on column values [message #610715 is a reply to message #610710] Sun, 23 March 2014 09:27 Go to previous message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
its okay , thanks dariyoosh.I will try to find out a way.
Previous Topic: sql query logic
Next Topic: Time Elapsed
Goto Forum:
  


Current Time: Thu Apr 25 20:16:15 CDT 2024