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

SELECT the key whose column has all the input list of values


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
V S Amarendra Reddy

Active User


Joined: 13 Sep 2006
Posts: 216
Location: USA

PostPosted: Mon Jul 30, 2012 7:05 pm
Reply with quote

Hi,

My Table has the values below.

Code:
Key         Origin
-----        -------
1              CS
1              CO
2              AB
2              BC
2              CO
3              CO
3              AB
4              CO
4              CS
My requirement is to get the keys who Origin contains both CO,AB. So for this case I am expecting keys 2,3 to be returned to the output.

I tried to search forum for different usages of predicates ALL and selecting DISTINCT. But I couldn't find much.

Can some one help please?

Regards
Amar

Code'd
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Mon Jul 30, 2012 7:38 pm
Reply with quote

Hello,
Will this work out?
Code:

SELECT DISTINCT KEY FROM TABLE1 WHERE ORIGIN = 'CO'   
INTERSECT                                             
SELECT DISTINCT KEY FROM TABLE1 WHERE ORIGIN = 'AB'; 
Back to top
View user's profile Send private message
V S Amarendra Reddy

Active User


Joined: 13 Sep 2006
Posts: 216
Location: USA

PostPosted: Mon Jul 30, 2012 8:44 pm
Reply with quote

Hi Vasanth,

Thank you. It is working. But if we have more values in the input, we got to keep adding the queries. Would there be any alternate way possible to provide the list of values.

Regards
Amar
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Mon Jul 30, 2012 8:48 pm
Reply with quote

This appears to be another case of "stone soup" requirements. icon_axe.gif
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: Mon Jul 30, 2012 9:31 pm
Reply with quote

Hello,

Quote:
But if we have more values in the input, we got to keep adding the queries. Would there be any alternate way possible to provide the list of values
You could always simply write a program that would input the values at run-time and go on from there. . .
Back to top
View user's profile Send private message
V S Amarendra Reddy

Active User


Joined: 13 Sep 2006
Posts: 216
Location: USA

PostPosted: Mon Jul 30, 2012 10:15 pm
Reply with quote

Hi,

Programming is also an option. But thinking to keep it as last option, if I can't get any alternate solution at all for this requirement.

Thanks for suggestion.

Regards
Amar
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Jul 31, 2012 1:29 pm
Reply with quote

If (Key,Origin) is a unique key, you could code it like this. But I doubt it will be faster :
Code:
select key from table1 where origin in ('CO','AB')
group by key
having count(*) = 2
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 TRIM everything from input, output co... DFSORT/ICETOOL 1
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
Search our Forums:

Back to Top