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

Joint query


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

Active User


Joined: 18 Jan 2010
Posts: 143
Location: Pune

PostPosted: Fri May 21, 2010 10:55 am
Reply with quote

Hi Team,
I have two table:
Table A and Table B
Both have follwing three fields in common:
Employee Number :
IN A defined as decimal
IN B defined as CHar(10)

Emplye Code:
Table A and B defined as X(1)

Emp id:
Table A: As S9(7) comp-3
Table B: as char(12)

my query should give only those combination of 3 varibles from table which are not present in Table -B

I used following query:
SELECT
A.EMPNO,
A.EMPCODE,
A.EMPID
FROM EMPA A, EMPPA B
WHERE A.EMPNO <> CAST(B.EMPNO AS DECIMAL(10)) AND
A.EMPCODE <> B.EMPCODE AND
A.EMPID <> CAST(B.EMPID AS DECIMAL(12))


ABOVE query results in output which are present in the table b. It contradicts my requirement.
May you please guide where i am going wrong.

Regards,
Krunal
Back to top
View user's profile Send private message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Fri May 21, 2010 11:47 am
Reply with quote

select CAST(B.EMPNO AS DECIMAL(10)) ,B.EMPCODE , CAST(B.EMPID AS DECIMAL(12))
with above selected value and compare both ......you will get the reason for the same
Back to top
View user's profile Send private message
krunalbafna
Warnings : 1

Active User


Joined: 18 Jan 2010
Posts: 143
Location: Pune

PostPosted: Fri May 21, 2010 11:56 am
Reply with quote

HI Bhairon,
As you mentioned i used the above query and can find that values taken for the comaparision are different.

Now how can we execute or refine the query to get only those records from table A which are not present in table B depending on combination
Employee Number,EMP CODE and EMP ID
Back to top
View user's profile Send private message
bhairon singh rathore

New User


Joined: 19 Jun 2008
Posts: 91
Location: banglore

PostPosted: Fri May 21, 2010 12:10 pm
Reply with quote

Hi Krunal,

Can you please show some of the result with both values in select.
Back to top
View user's profile Send private message
krunalbafna
Warnings : 1

Active User


Joined: 18 Jan 2010
Posts: 143
Location: Pune

PostPosted: Fri May 21, 2010 12:13 pm
Reply with quote

from table B it is only taking same values
A.EMPNO B.EMPNO A.EMPCODE A.EMPID
008 171 Z 303010
086 171 F 7757
086 171 L 858009
110 171 L 7758460
110 171 S 2984821
110 171 Z 7678571
167 171 S 17946669
167 171 Z 81336697
133 171 S 13608461
133 171 Z 00597832
Back to top
View user's profile Send private message
krunalbafna
Warnings : 1

Active User


Joined: 18 Jan 2010
Posts: 143
Location: Pune

PostPosted: Fri May 21, 2010 1:25 pm
Reply with quote

Hi team
Any suggestion or help...
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri May 21, 2010 2:06 pm
Reply with quote

this join will create a Cartesian product except the inner join on the three columns :
table 1 table2
row1 row1
row2 row2
row3 row3

row1 joined row2 qualifies your where clause
row1 joined row3 qualifies your where clause
row2 joined row1 qualifies your where clause
row2 joined row3 qualifies your where clause
row3 joined row1 qualifies your where clause
row3 joined row2 qualifies your where clause

what you probably want is something like EXCEPT in DB2 V9
or where not exists() pre V9

Code:
select dbname, name from sysibm.systablespace
except
select dbname, tsname from sysibm.systables
with ur

Code:
select * from sysibm.systablespace TS
where not exists (select * from sysibm.systables TB
where TB.dbname = TS.DBNAME and TB.tsname = TS.NAME)
with ur


will give you everything from tablespace which isn't in tables
Back to top
View user's profile Send private message
mallik4u

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Fri May 21, 2010 4:38 pm
Reply with quote

Hi GUYC,

Thanks for giving information about EXCEPT keyword. It is really very usefull.
Can you share any document/url on new/updated db2 v9?
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri May 21, 2010 5:27 pm
Reply with quote

except: publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.apsg/db2z_combineresultmultipleselect.htm

what's New : publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc/db29lib.htm

Redbooks
Technical : www.redbooks.ibm.com/abstracts/SG247330.html
Performance : www.redbooks.ibm.com/abstracts/sg247473.html
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top