Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

SUB QUERIES - delete
Goto page 1, 2  Next
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: SUB QUERIES - delete
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    Post subject:
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    Post subject:
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    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Fri May 23, 2008 1:38 pm    Post subject: Reply to: SUB QUERIES - delete
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    Post subject:
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    Post subject:
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

Site Director


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

PostPosted: Sat May 24, 2008 11:00 pm    Post subject:
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    Post subject:
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    Post subject:
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

Site Director


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

PostPosted: Mon May 26, 2008 3:10 am    Post subject: Reply to: SUB QUERIES - delete
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

Site Director


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

PostPosted: Mon May 26, 2008 6:07 am    Post subject: Reply to: SUB QUERIES - delete
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    Post subject:
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

Site Director


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

PostPosted: Mon May 26, 2008 6:51 am    Post subject: Reply to: SUB QUERIES - delete
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts JCL to delete component in a package sundaram.naveen Compuware & Other Tools 14 Tue Nov 29, 2016 6:21 pm
No new posts How to delete second instance from Fl... Gunapala CN DFSORT/ICETOOL 6 Tue Oct 18, 2016 11:42 pm
No new posts Delete record or Reset RC code using ... mpawan Compuware & Other Tools 4 Tue Jul 26, 2016 3:52 pm
No new posts Delete VSAM File Records amitc23 CICS 6 Sun Apr 10, 2016 10:10 am
No new posts DB2 Stored Procedure - Dynamic SQL - ... GuyC DB2 2 Wed Feb 17, 2016 5:10 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us