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: 1712
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: 10201
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
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
No new posts Match or compare two files in VB Format anatol DFSORT/ICETOOL 14 Thu Nov 03, 2016 7:41 pm
No new posts Access SQLCA fields inside a stored p... gangapd DB2 4 Thu Oct 27, 2016 10:20 am
No new posts Problem in Running Query via JCL vickey_dw DB2 3 Tue Oct 18, 2016 11:11 pm


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