IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SUB QUERIES - delete


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Fri May 23, 2008 12:21 pm
Reply with quote

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
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Fri May 23, 2008 1:13 pm
Reply with quote

can you plz explain with some examples...
Back to top
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Fri May 23, 2008 1:31 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri May 23, 2008 1:33 pm
Reply with quote

sorta like delete in table 1 where exists in table 2?
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10888
Location: italy

PostPosted: Fri May 23, 2008 1:38 pm
Reply with quote

Quote:
sorta like delete in table 1 where exists in table 2?


please be more specific, dfsorta or syncsorta icon_biggrin.gif
Back to top
View user's profile Send private message
chandu.be

New User


Joined: 17 Jul 2006
Posts: 9
Location: Bagalore

PostPosted: Fri May 23, 2008 4:00 pm
Reply with quote

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
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Sat May 24, 2008 4:41 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Sat May 24, 2008 11:00 pm
Reply with quote

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
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Sun May 25, 2008 3:13 pm
Reply with quote

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
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Sun May 25, 2008 10:42 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Mon May 26, 2008 3:10 am
Reply with quote

Thank you - my bad - i've been reading the last rule wrong. Once i got the wrong focus, i got stuck there icon_redface.gif

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Mon May 26, 2008 6:07 am
Reply with quote

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
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Mon May 26, 2008 6:32 am
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Mon May 26, 2008 6:51 am
Reply with quote

Hi Gautam,
Quote:
I am a bit confused here.
I'm confused trying to decide if i'm confused. . . icon_confused.gif

d
Back to top
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Mon May 26, 2008 10:16 am
Reply with quote

Hope you make a decision sooner... icon_eek.gif
Back to top
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Mon May 26, 2008 2:49 pm
Reply with quote

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
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Mon May 26, 2008 4:03 pm
Reply with quote

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
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Mon May 26, 2008 4:27 pm
Reply with quote

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
View user's profile Send private message
the_gautam

Active User


Joined: 05 Jun 2005
Posts: 165
Location: Bangalore

PostPosted: Tue May 27, 2008 7:05 am
Reply with quote

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
View user's profile Send private message
Raja12752

New User


Joined: 18 Jul 2006
Posts: 28

PostPosted: Wed May 28, 2008 11:23 am
Reply with quote

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
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed May 28, 2008 2:58 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts DELETE SPUFI DB2 1
No new posts DSNTIAUL driven delete IBM Tools 0
No new posts How to delete a user's alias from the... JCL & VSAM 11
No new posts Delete file row if blanks in the firs... DFSORT/ICETOOL 5
No new posts volume mass delete RMM JCL & VSAM 2
Search our Forums:

Back to Top