Max value among three columns

 

The following query will fetch you the max value among three columns
Create Table #test (Name Char(10), Qty1 INT, QTY2 INT, QTY3 INT)

Insert into #Test (Name, QTY1, QTY2, QTY3) Values (‘Monitor’,12,13,11)

Insert into #Test (Name, QTY1, QTY2, QTY3) Values (‘KeyBoard’,8,7,6)

Insert into #Test (Name, QTY1, QTY2, QTY3) Values (‘Mouse’,3,2,5)

–Query to get the max among three columns

select [name],(select max(Qty)

                     from   (select QTY1 as Qty

                             union all

                             select QTY2 as Qty

                             union all

                             select QTY3) as Qty) as Maxi

from #test

 

The URI to TrackBack this entry is: http://dayananthan.wordpress.com/2008/06/12/max-value-among-three-columns/trackback/

RSS feed for comments on this post.

One Comment Leave a comment.

  1. Real real good query….


Leave a Comment