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

Want to compare 2 fields in the same table through query


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

Active User


Joined: 27 Feb 2008
Posts: 110
Location: india

PostPosted: Tue Apr 23, 2013 12:25 pm
Reply with quote

I want to compare 2 fields in the same table through a db2 query.

1. when the TYPE = 'X' that had a DEST equal to that when the TYPE = 'Y' .
All the above required data available in one table.

If i write the query in WHERE statement to satisfy the above condition, then


SELECT * FROM CUSTOMER C
WHERE ( (C.TYPE = 'X') = (C.TYPE = 'Y') AND (C.DEST = C.DEST) )


it is not working. Anyone want to check this? Thanks in advance
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Tue Apr 23, 2013 1:37 pm
Reply with quote

Looks like you are trying to see if TYPE has both values 'X' and 'Y' at the same time. Also C.DEST will always = C.DEST.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Apr 23, 2013 1:50 pm
Reply with quote

I interpret it as
DEST of the Y row is equal to the DEST of the X row


something like ... ( the syntax might be blatantly wrong, just hinting the logic )
select * from table a
where a.type = 'X"
union
select * from table b
where b.type = 'y'
and b.dest = a.dest

somebody with better DB2 reminiscences might provide the correct one
icon_biggrin.gif
Back to top
View user's profile Send private message
maxsubrat

Active User


Joined: 27 Feb 2008
Posts: 110
Location: india

PostPosted: Tue Apr 23, 2013 3:01 pm
Reply with quote

Want to fetch records based on where the 'X' TYPE record that had a DEST value equal to that with 'Y' TYPE record.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


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

PostPosted: Tue Apr 23, 2013 9:12 pm
Reply with quote

Hello,

Suggest you show some sample rows (only the columns that matter for this question) and show what the quesy should select.

The sample rows should have some rows to be selected and some that will not be selected.

Your description may be clear to you, but it is not so clear to others.
Back to top
View user's profile Send private message
maxsubrat

Active User


Joined: 27 Feb 2008
Posts: 110
Location: india

PostPosted: Wed Apr 24, 2013 10:55 am
Reply with quote

for example: table A
Code:
Invoice     ORIGIN                   DEST       TYPE 
02345       MUMBAI                  CHENNAI    I
03456       PUNE                       HYD           I
09999       KOLKATA                  DELHI        E
08888       CHENNAI                  PUNE          E


The condition is:
I want to fetch the records based on: The ORIGIN of 'E' TYPE equal to the DEST of 'I' TYPE

in the above example, i think 2 records matching and want to fetch these records...

If you need more info, please let me know... Thanks
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Thu Apr 25, 2013 1:22 am
Reply with quote

maxsubrat wrote:
for example: table A
Code:
Invoice     ORIGIN                   DEST       TYPE 
02345       MUMBAI                  CHENNAI    I
03456       PUNE                       HYD           I
09999       KOLKATA                  DELHI        E
08888       CHENNAI                  PUNE          E


The condition is:
I want to fetch the records based on: The ORIGIN of 'E' TYPE equal to the DEST of 'I' TYPE

in the above example, i think 2 records matching and want to fetch these records...

If you need more info, please let me know... Thanks

This isn't tested but I think it will do it

Code:
SELECT A2.INVOICE, A2.ORIGIN, A2.DEST, A2.TYPE
FROM TABLEA A1, TABLEA A2
WHERE A1.TYPE = 'I'
AND A2.TYPE = 'E'
AND A2.ORIGIN = A1.DEST;


If the table is large and doesn't have the necessary indexes it will be slow.
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 Load new table with Old unload - DB2 DB2 6
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts RC query -Time column CA Products 3
No new posts Multiple table unload using INZUTILB DB2 2
Search our Forums:

Back to Top