MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unique Values from 3 cols from a selection list of 5 cols

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
bullfighter
Beginner


Joined: 15 Apr 2005
Posts: 26
Topics: 9

PostPosted: Fri May 20, 2005 2:29 pm    Post subject: Unique Values from 3 cols from a selection list of 5 cols Reply with quote

I have an sql like below
I need to select COL1, COL2, COL3, COL4, COL5,
but only unique COL1, COL2, COL3

If I use DISTINCT after SELECT, it is giving unique of all the columns but not the unique of COL1, COL2, COL3, please suggest me how to do
Code:


SELECT COL1  ,
       COL2 ,
       COL3 ,
       COL4 ,
       COL5
FROM TAB1.TABLE
WHERE COL1 = "ABC"

Thanks in advance
_________________
champion is not one who never fails, but one who never quits
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Fri May 20, 2005 2:51 pm    Post subject: Reply with quote

Bullfighter,

If you want to pick up distinct values of col1 ,col2 and col3 you should use a scalar/column function on the other 2 columns.

I will explain with an example. let us assume that your table looks like this

Code:

---------+---------+---------+---------+----
COL1  COL2  COL3         COL4         COL5 
---------+---------+---------+---------+----
ABC   BB    C               1            2 
ABC   BB    C               3            4 
ABC   BB    C               5            6 
DEF   CC    D               7            8 
DEF   CC    D               9            0 
DEF   CC    D               1            2 



Now if you want to pick an unique value for col1= abc, what values of col4 and col5 will you pick?

That is where you will have a problem. do you want to pick the max or min for col4 and col5?

Tell us which column you want to pick col4 and col5 and we will give you a solution.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
bullfighter
Beginner


Joined: 15 Apr 2005
Posts: 26
Topics: 9

PostPosted: Fri May 20, 2005 2:54 pm    Post subject: Reply with quote

I will do an ORDER BY COL5 DESC
and pick what ever comes in the first COL4 and COL5 for unique ( COL1, COL2, COL3 )

thanks
_________________
champion is not one who never fails, but one who never quits
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Fri May 20, 2005 3:25 pm    Post subject: Reply with quote

Quote:

I will do an ORDER BY COL5 DESC and pick what ever comes in the first COL4 and COL5 for unique ( COL1, COL2, COL3 )



Code:

SELECT COL1                                   
      ,COL2                                   
      ,COL3                                   
      ,COL4                                   
      ,COL5                                   
  FROM TAB1.TABLE
 WHERE (COL1,COL2,COL3,COL5) IN (SELECT COL1
                                       ,COL2
                                       ,COL3
                                       ,MAX(COL5)
                                   FROM TAB1.TABLE
                                  WHERE COL1 = 'ABC'
                                  GROUP BY COL1
                                          ,COL2
                                          ,COL3)
;


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
bullfighter
Beginner


Joined: 15 Apr 2005
Posts: 26
Topics: 9

PostPosted: Fri May 20, 2005 6:16 pm    Post subject: Reply with quote

this is cool kolusu, thanks
_________________
champion is not one who never fails, but one who never quits
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group