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

Compare columns of same table


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

New User


Joined: 02 Sep 2006
Posts: 31
Location: Mumbai

PostPosted: Tue Sep 08, 2009 5:12 pm
Reply with quote

Hi,

My table has 5 similar columns.
For each record in table, I want the data of the column having the max value.
E.g.
Row 1 : ABC 5 7 8 3 1
Row 2 : PQR 9 4 0 3 5

Output
ABC 8
PQR 9

Has anyone come across such situation, where you need to compare columns of same table.

Thanks,
Prasad.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Sep 08, 2009 5:43 pm
Reply with quote

Code:
select col1,max(col2,col3,col3,col4) from table1

should work
Back to top
View user's profile Send private message
prasadplease

New User


Joined: 02 Sep 2006
Posts: 31
Location: Mumbai

PostPosted: Tue Sep 08, 2009 6:54 pm
Reply with quote

Wow!!!

I really had no clue that max also works on columns....

Thanks
GuyC

Now next problem is if any column has nulls it's value is returned as max...
Back to top
View user's profile Send private message
Bharath Bhat

Active User


Joined: 20 Mar 2008
Posts: 283
Location: chennai

PostPosted: Tue Sep 08, 2009 7:24 pm
Reply with quote

Code:
select col1,max(coalesce(col2,0),col3,col3,col4) from table1


I've assumed only col2 to be nullable. You can do the same for other columns as well.
Back to top
View user's profile Send private message
prasadplease

New User


Joined: 02 Sep 2006
Posts: 31
Location: Mumbai

PostPosted: Thu Sep 10, 2009 1:31 pm
Reply with quote

Thanks Bharath, GuyC
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Sat Sep 12, 2009 7:37 am
Reply with quote

Bharath Bhat wrote:
Code:
select col1,max(coalesce(col2,0),col3,col3,col4) from table1


I've assumed only col2 to be nullable. You can do the same for other columns as well.


You had used Max(c1, c2, ...cj) - which is scalar function,
but you may get duplicates.

So, to this function you have to use MAX - column function.

Solution could be:

Code:
select tabid, MAX(Max(c1, c2, ...cj) )
from table1
group by tabid


Thanks, Lenny.
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Sat Sep 12, 2009 8:06 am
Reply with quote

How I see, I have master degree on troubles.... icon_redface.gif

Better to use next SQL:

Code:
Select tabid, Max(MaxC ) MMaxC
from
(select tabid, Max(c1, c2, ...cj) MaxC
   from table1 ) mx
group by tabid


Lenny
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 Remote Unload of CLOB Columns DB2 6
No new posts Multiple table unload using INZUTILB DB2 2
Search our Forums:

Back to Top