Home » SQL & PL/SQL » SQL & PL/SQL » Merge on tables (11.2.0.3)
Merge on tables [message #640745] Tue, 04 August 2015 13:33 Go to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
Hi all,

I have two tables

CREATE table W_S_SCORE
(
F_CODE  Varchar2(3),
E_SCORES varchar2(2)
S_SCORE varchar2(2)
);
 
INSERT INTO W_S_SCOREvalues ('12A', '08','01');
INSERT INTO W_S_SCOREvalues ('12A', '09','04');
INSERT INTO W_S_SCOREvalues ('12A', '10','05');
INSERT INTO W_S_SCOREvalues ('12A', '07','05');
INSERT INTO W_S_SCOREvalues ('12C', '08','01');


Table 2 :
 
CREATE TABLE A_STUD
(
l_code varchar2(6),
f_code varchar2(3),
E_SCORE VARCHAR2(2),
S_SCORES VARCHAR2(2)
);
 
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2028','12A','08');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2029','12C','19');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2030','12C','20');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2031','12C','10');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2032','12A','08');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2037','12C','07');
INSERT INTO A_STUD (l_code ,f_code ,E_SCORE) VALUES ('2038','12C','10');


TABLE HAS DATA AS FOLLOWS:

l_code f_code       E_sCORES    S_SCORE
---------------------------------------------------------
2028    12A            08

2029    12C            19

---
---
---

nOW REQUIRED O/P SHOULD BE , S_SCORE COLUMN SHOULD UPDATE BASED ON E_SCORES DATA

l_code  f_code       E_sCORES    S_SCORE
---------------------------------------------------------
2028    SCOTT          08            01

2029     GARMIN       19              13

{CODE}

If, In A_STUD table  S_SCORE is missing then it should retrieve S_SCORE value automatically from W_S_SCORE table based on the E_SCORE in A_STUD table.



Thanks,
Vasudev
Re: Merge on tables [message #640747 is a reply to message #640745] Tue, 04 August 2015 13:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
why are you storing same data in 2 different table?
this "design" is seriously flawed!
What is the score values are different between the two tables?
Re: Merge on tables [message #640749 is a reply to message #640745] Tue, 04 August 2015 14:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Where do "SCOTT" and "GARMIN" come from?

Re: Merge on tables [message #640751 is a reply to message #640749] Tue, 04 August 2015 14:57 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
It's copy n pate errors..

those fields are for f_code... the values are like 12A,12c only

let me know if additional information required.
Re: Merge on tables [message #640752 is a reply to message #640751] Tue, 04 August 2015 15:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So the output you gave has nothing to do with the input you gave.
So you want we give a query when we don't know what you want.

Copy and paste? Copy and paste what you have no query to give the result?

You should FIRST carefully read and read again your question before posting it and don't waste our time.

So back to the question, give the correct result from your test case and explain every column of every line from it.

Re: Merge on tables [message #640753 is a reply to message #640747] Tue, 04 August 2015 15:03 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
As per our design Need to maintain S_SCORE, F_CODE, E_SCORE in one table i.e W_S_SCORE.

In the other table We called S_SCORE as S_SCORES i.t A_STUD table.

this column S_SCORES need to get the values ( updated values ) from W_S_SCORE table based on the E_SCORE values,

So I am trying to mrite merge statement.

But didn't see any Unique column in W_S_SCORE table. So unable to Merge the tables. Please help me out to UPDATE S_SCORES column in A_STUD table based on the W_SCORE table.

thanks,
Vasudev

Re: Merge on tables [message #640754 is a reply to message #640753] Tue, 04 August 2015 15:11 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member

l_code  f_code       E_sCORES    S_SCORE
---------------------------------------------------------
2028    SCOTT          08            01

2029     GARMIN       19              13



Out of which hat did you, the magician, pull the 13 above?
IMO, your imagination GREATLY exceeds your (lack of) technical ability.
Re: Merge on tables [message #640755 is a reply to message #640752] Tue, 04 August 2015 15:19 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
Please find the below data and the requirement.

Table1 :  A_STUD		

L_CODE	F_CODE	E_SCORE	W_S_SCORE
935	10B	8	
1033	10B	8	
1029	10B	8	
904	10B	9	
932	10B	9	
1040	10B	8	
966	10B	9	
715	10B	10	
1053	10B	22	
938	10B	31	


Table 2 : W_S_SCORES
--------
F_CODE	E_SCORE	S_SCORE 
12A	8	1
12A	9	5
12A	10	6
12A	11	7
12A	12	8
12A	13	10
12A	14	10
12A	15	11
12A	16	11
12A	17	12
12A	18	13
12A	19	14
12A	20	14
12A	21	15
12A	30	23
12C	9	4
12C	10	5
12C	11	6
12C	12	7
12C	13	9
12C	14	9
12C	15	10
12C	16	10
12C	17	11


1. In A_STUD table W_S_SCORE column is EMPTY.

2. We have S_SCORE values in W_S_SCORES table.

Now we need to Merge these two tables and need to update W_S_SCORE in A_STUD table with S_SCORE values from W_S_SCORE table. The output should be as follows.

If you compare both A_STUD table and W_S_SCORE tables,

for E_SCORE ' 8' S_SCORE value is '1' from W_S_SCORES table.
for E_SCORE ' 9' S_SCORE value is '4'
for E_SCORE ' 10' S_SCORE value is '5'
for E_SCORE ' 22' S_SCORE value is '15'
for E_SCORE ' 31' S_SCORE value is '23'

like this we have so many E_SCORES and they have respective S_SCORES.


so required O/P is
A_STUD

L_CODE	F_CODE	E_SCORE	W_S_SCORE
935	10B	8	 1
1033	10B	8	 1
1029	10B	8	 1
904	10B	9        4	
932	10B	9	 4
1040	10B	8	 1
966	10B	9	 4
715	10B	10	 5
1053	10B	22	 15
938	10B	31       23

Re: Merge on tables [message #640756 is a reply to message #640754] Tue, 04 August 2015 15:21 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
@SWAN


l_code  f_code       E_sCORES    S_SCORE
---------------------------------------------------------
2028      12A          08            01

2030      10B          09            04
2032      10B          10            05
2034      10C          8             01
2036      10C          22            15
Re: Merge on tables [message #640757 is a reply to message #640756] Tue, 04 August 2015 15:34 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
vasudevaviswa wrote on Tue, 04 August 2015 13:21
@SWAN


l_code  f_code       E_sCORES    S_SCORE
---------------------------------------------------------
2028      12A          08            01

2030      10B          09            04
2032      10B          10            05
2034      10C          8             01
2036      10C          22            15


I give up.
What exactly is above?
From where did/does "10C" originate?
I suggest that you stop wasting your time & our time posting random characters.

Do you speak SQL?
If so then actually post functional SQL statements; like INSERT & SELECT
Re: Merge on tables [message #640761 is a reply to message #640757] Tue, 04 August 2015 16:19 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
F_CODE has different codes. 10C,12A,12B,10C,10B.. etc.. are F_CODES... these are coming from W_S_SCORE table.


I have posted different F_CODE here..

Let me know what other information you required...

Posted the table structure and some of the data.. which is existed in the given 2 tables..





[Updated on: Tue, 04 August 2015 16:21]

Report message to a moderator

Re: Merge on tables [message #640762 is a reply to message #640752] Tue, 04 August 2015 17:22 Go to previous messageGo to next message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
I have written the belwo MERGE statement. But it is throwing error as E_STUD table doesn't have any PK constraint.

MERGE INTO  E_stud   dst
USING (
          SELECT distinct a.f_code
          ,       e.s_score
          FROM    E_stud   a
          JOIN    E_SCORES  e  ON  e.e_score  = a.e_score
      )              src
ON    (dst.f_code  = src.f_code)
WHEN MATCHED THEN UPDATE
SET   dst.w_s_score  = src.s_score
;


Here is the error message which am getting

ORA-30926: unable to get a stable set of rows in the source tables

Any one please advice.
Re: Merge on tables [message #640763 is a reply to message #640762] Tue, 04 August 2015 18:15 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Why using MERGE?
Why not use plain UPDATE?

>If, In A_STUD table S_SCORE is missing
if above is true, why is NOT included in your SQL statement?

>But it is throwing error as E_STUD table doesn't have any PK constraint.
What is relationship between table E_STUD & A_STUD?
Re: Merge on tables [message #640840 is a reply to message #640763] Wed, 05 August 2015 14:28 Go to previous message
vasudevaviswa
Messages: 53
Registered: August 2015
Location: US
Member
Hi All,


Appreciate your time. The plain update statement is working fine as suggested by you experts.

Thanks,
Vasudev.
Previous Topic: Need modification in Stored Procedure.
Next Topic: Convert rows into columns
Goto Forum:
  


Current Time: Fri Apr 26 22:49:17 CDT 2024