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
 

 

Want to compare 2 fields in the same table through query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
maxsubrat

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Tue Apr 23, 2013 12:25 pm    Post subject: Want to compare 2 fields in the same table through query
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: 1722
Location: UK

PostPosted: Tue Apr 23, 2013 1:37 pm    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10211
Location: italy

PostPosted: Tue Apr 23, 2013 1:50 pm    Post subject: Reply to: Want to compare 2 fields in the same table through
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

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Tue Apr 23, 2013 3:01 pm    Post subject: Reply to: Want to compare 2 fields in the same table through
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

Site Director


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

PostPosted: Tue Apr 23, 2013 9:12 pm    Post subject:
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

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Wed Apr 24, 2013 10:55 am    Post subject: Reply to: Want to compare 2 fields in the same table through
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    Post subject: Re: Reply to: Want to compare 2 fields in the same table thr
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts SYNCSORT SEQNUM and FI fields. nartcr SYNCSORT 5 Sat Jan 21, 2017 4:02 am
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
No new posts Storing huge volume of data, compare ... Pradeep K M All Other Mainframe Topics 3 Mon Jan 16, 2017 5:08 pm
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm


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