Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Find lowest value in 3 columns

Author  Topic 

nhess80
Yak Posting Veteran

83 Posts

Posted - 2008-06-02 : 14:09:17
Im having trouble finding a solution to comparing columns to find the lowest value.

Example

Row 1

COL 1 = 10
COL 2 = 20
COL 3 = 60
COL 4 = 5
COL 5 = 35

I would want to return 5.

I tried to write a case statement such as below but am unable to use Logical Operators.

WHEN C1 <= C2 and C1<= C3 THEN C1
WHEN C2 <= C3 THEN C2
ELSE C3



any help would be great.

Thanks,
Nick

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-02 : 14:15:16
[code]SELECT t.PKcol,MIN(t.ColValue)
FROM
( SELECT PKCol,Col1 AS ColValue
FROM YourTable

UNION ALL

SELECT PKCol,Col2
FROM YourTable

UNION ALL

SELECT PKCol,Col3
FROM YourTable

UNION ALL

SELECT PKCol,Col4
FROM YourTable

UNION ALL

SELECT PKCol,Col5
FROM YourTable
)t
GROUP BY t.PKCol[/code]

Go to Top of Page
   

- Advertisement -