Max value among three columns

Posted: June 12, 2008 in Important SQL Query, Interview Questions, SQL Server, Utlities
Tags: , , ,

 

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

 

Advertisement
Comments
  1. Amit says:

    Real real good query….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s