Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Compare columns of same table

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Compare columns of same table
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    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 Compare VSAM files using DFSORT pshongal DFSORT/ICETOOL 5 Thu Nov 23, 2017 10:24 am
No new posts Can EDCICONV convert EBCDIC to ASCII... natt.sut IBM Tools 3 Fri Nov 17, 2017 8:46 pm
No new posts Compare and COPY using DFSORT pshongal DFSORT/ICETOOL 3 Fri Nov 17, 2017 9:49 am
No new posts Copy selected columns from 2 input fi... Poha Eater DFSORT/ICETOOL 3 Thu Nov 02, 2017 3:43 pm
No new posts Update table without cursor Ron Klop DB2 3 Wed Oct 11, 2017 1:06 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us