View previous topic :: View next topic
|
Author |
Message |
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
I have a requirement,
TABLE1 - COL1, COL2, COL3, COL4, COL5
TABLE2 - COL1, COL2, COL3, COL4, COL5
Would like to delete the TABLE1 data when
TABLE1.COL = TABLE2.COL and
If any of other columns not match.
Thanks. |
|
Back to top |
|
|
the_gautam
Active User
Joined: 05 Jun 2005 Posts: 165 Location: Bangalore
|
|
|
|
can you plz explain with some examples... |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
Table1
------
T1P1
T2P1
T2P2
T3P1
Table2
-------
T1P1
T2P1
T2P3
Result TABLE1:
--------------
T2P2
T3P1
As the First 4 columns are Key for both tables.
Thanks. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
sorta like delete in table 1 where exists in table 2? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
Quote: |
sorta like delete in table 1 where exists in table 2? |
please be more specific, dfsorta or syncsorta |
|
Back to top |
|
|
chandu.be
New User
Joined: 17 Jul 2006 Posts: 9 Location: Bagalore
|
|
|
|
Hi,
i hope following query will work for your requirement.
delete * from table1
inner join table2 on
table1.col1 = table2.col1 and
(table1.col2 <> tabl2.col2 or
table1.col3 <> tabl2.col3 or
table1.col4 <> tabl2.col4 or
table1.col5 <> tabl2.col5
) |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
It is not working properly.
Can some one fine tune this query.....the requirement is simple,
Two tables where First column should be equal and in rest of 4columns any of one is not equal, we have to delete that record.
Thanks. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
the requirement is simple, |
Only when the requirement is understood. You have not clearly shown your requirement. You need to post a better set of input and output data. Include the 5 columns in both sets of input and the complete result rather than the keys.
Keep in mind that your requirement is completely clear to you, but it has not been to some who read it. |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
ok....
The Table A
--------------
T1 P1 0 0 1
T1 P2 0 0 1
T1 P3 0 0 1
T2 P1 0 0 1
T2 P2 0 0 1
T3 P1 0 0 1
The Table B
------------
T1 P1 0 0 1
T1 P2 0 0 1
T2 P1 0 0 1
Output Table A
----------------
T1 P1 0 0 1
T1 P2 0 0 1
T2 P1 0 0 1
T3 P1 0 0 1
First Column T1 found in Both Table A & B, the corresponding P1, P2 only found in Table B. So, the Output Table A has T1 related 2 recorrds.
T2 related only P1 record found in Table B. So, T2 related 1 record present in Ouput Table A.
T3 is not at all present in Table B. So, it is present in Ouput table A.
Thanks. |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
I will try to explain once again......
Code: |
The Table A The Table B Output Table A
------------- ------------ ----------------
T1 P1 0 0 1 T1 P1 0 0 1 T1 P1 0 0 1
T1 P2 0 0 1 T1 P2 0 0 1 T1 P2 0 0 1
T1 P3 0 0 1 T2 P1 0 0 1 T2 P1 0 0 1
T2 P1 0 0 1 T3 P1 0 0 1
T2 P2 0 0 1
T3 P1 0 0 1 |
First it should need to check the TableA.Col1 = TableB.Col1
If this satisfy then only the rest of columns need to check.
If any of the other column in a Record didn't find in Table-B then we have to delete in Table-A.
Thanks, |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Thank you - my bad - i've been reading the last rule wrong. Once i got the wrong focus, i got stuck there
Back to the earlier suggestion:
Quote: |
i hope following query will work for your requirement.
delete * from table1
inner join table2 on
table1.col1 = table2.col1 and
(table1.col2 <> tabl2.col2 or
table1.col3 <> tabl2.col3 or
table1.col4 <> tabl2.col4 or
table1.col5 <> tabl2.col5
) |
Quote: |
It is not working properly. |
How does this code fail? Does it delete the wrong rows? Does it delete anything?
I'm away from the system i work with that has db2 for a couple of weeks and prefer to not post untested queries.
Again, sorry for the confusion,
d |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Things appear to have changed between Fri May 23, 2008 2:01 am and earlier today (Sun May 25). Actually, your posted sample input data is not the same for the 2 posts today - which are both not the same as the original. . .
I've removed my post from between your 2 to hopefully make this easier to read.
d |
|
Back to top |
|
|
the_gautam
Active User
Joined: 05 Jun 2005 Posts: 165 Location: Bangalore
|
|
|
|
Code: |
The Table A The Table B Output Table A
------------- ------------ ----------------
T1 P1 0 0 1 T1 P1 0 0 1 T1 P1 0 0 1
T1 P2 0 0 1 T1 P2 0 0 1 T1 P2 0 0 1
T1 P3 0 0 1 T2 P1 0 0 1 T2 P1 0 0 1
T2 P1 0 0 1 T3 P1 0 0 1
T2 P2 0 0 1
T3 P1 0 0 1 |
I am a bit confused here.
Code: |
T1 P1 0 0 1 T1 P1 0 0 1
T1 P2 0 0 1 T1 P2 0 0 1
T2 P1 0 0 1 T2 P1 0 0 1 |
These rows are common in both the tables and are there in the resultant table as well.
However, what about
Code: |
T3 P1 0 0 1 T3 P1 0 0 1 |
This is also available in both the tables but not in the resultant table. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hi Gautam,
Quote: |
I am a bit confused here. |
I'm confused trying to decide if i'm confused. . .
d |
|
Back to top |
|
|
the_gautam
Active User
Joined: 05 Jun 2005 Posts: 165 Location: Bangalore
|
|
|
|
Hope you make a decision sooner... |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
Ya....T3 should present in Output tableA. It should not delete.
delete * from tableA
inner join tableB on
tableA.col1 = tableB.col1 and
(tableA.col2 <> tablB.col2 or
tableA.col3 <> tablB.col3 or
tableA.col4 <> tablB.col4
)
This query is throwing the wrong data. As there are negative condtions,
Is it possible to do every thing in One query??
Thanks. |
|
Back to top |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
Hi Raja,
Try this
Code: |
DELETE FROM TABLE1 A WHERE
A.COL1||A.COL2||CHAR(A.COL3)||CHAR(A.COL4)||CHAR(A.COL5) NOT IN
(SELECT B.COL1||B.COL2||CHAR(B.COL3)||CHAR(B.COL4)||CHAR(B.COL5)
FROM TABLE2 B);
|
|
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
This query will throw all the rows which are not present in Table-B.
But the query should need to proceed for only when TableA.Col1 = TableB.Col2.
This query may need to fine tune.
Thanks. |
|
Back to top |
|
|
the_gautam
Active User
Joined: 05 Jun 2005 Posts: 165 Location: Bangalore
|
|
|
|
Quote: |
But the query should need to proceed for only when TableA.Col1 = TableB.Col2 |
If this is the condition, can you please check the data provided by you?
None of the rows is satisfying the condition... |
|
Back to top |
|
|
Raja12752
New User
Joined: 18 Jul 2006 Posts: 28
|
|
|
|
Sorry....it should be TableA.Col1 = TableB.Col1.
Probably...thur Single query this requirement may not possible. Because there are 1positive and 4 negative conditions.
Finally we decide thru complete this by COBOL routine.
Thanks to all for your time. |
|
Back to top |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
Raja,
Your requirement can be dealt as the entire string present in table b should be kept in table a and the rest needs to be deleted.
The query which i have provided exactly gives the output you have provided in your prev posts... i have tested it too ...
and as you said the first 4 cols are keys for the table it should not deteriorate performance too ...
anyways good to hear that you have found an alternative ... |
|
Back to top |
|
|
|