Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating the minimum value in a set of columns

Status
Not open for further replies.

philclare

Programmer
Jul 31, 2003
34
GB
Hi,

Is there any way to select the minimum value in a set of columns in a single row.

eg. I have a table with 6 numeric fields, some of which may be null, and I want to find the lowest number in these fields for each row... I can find the first number by using COALESCE but not the lowest

Any ideas?

Thanks

Hope this helps,

Phil Clare
 
select min(minimum)
from
(select min(field1) as minimum from table1
union
select min(field2) as minimum from table1
union
select min(field3) as minimum from table1
union
select min(field4) as minimum from table1
union
select min(field5) as minimum from table1
union
select min(field6) as minimum from table1) minimumtbl

There must be an easier way but maybe this will work?
I would like to know if there is an easier way.

Atomic Wedgie

 
Well as it turns out my columns are in order so I can gut the lowest by using COALESCE (since that returns the first value that isn't null)

Thanks anyway

Hope this helps,

Phil Clare
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top