I'm trying to get the lowest of 3 column values but only if they are greater than 0. This is an example of my desired result:
+--------+--------+--------+--------+
| Hours1 | Hours2 | Hours3 | Lowest |
+--------+--------+--------+--------+
| 36.60 | 25.50 | 10.20 | 10.20 |
| -92.00 | 14.25 | 22.50 | 14.25 |
| -47.00 | -0.50 | 6.50 | 6.50 |
+--------+--------+--------+--------+
But this is what I am getting - the second and third rows are returning what is actually the lowest values - correct, but not actually what I need:
+--------+--------+--------+--------+
| Hours1 | Hours2 | Hours3 | Lowest |
+--------+--------+--------+--------+
| 36.60 | 25.50 | 10.20 | 10.20 |
| -92.00 | 14.25 | 22.50 | -92.00 |
| -47.00 | -0.50 | 6.50 | -47.00 |
+--------+--------+--------+--------+
So my question is, how can I modify my code to achieve the desired result? This is what I am using:
CREATE TABLE #t1 (Hours1 decimal(5,2),Hours2 decimal(5,2),Hours3 decimal(5,2))
INSERT INTO #t1 (Hours1, Hours2, Hours3) Values (36.6,25.5,10.2)
INSERT INTO #t1 (Hours1, Hours2, Hours3) Values (-92,14.25,22.5)
INSERT INTO #t1 (Hours1, Hours2, Hours3) Values (-47,-0.5,6.5)
SELECT Hours1
,Hours2
,Hours3
,(
CASE
WHEN Hours1 < Hours2
AND Hours1 < Hours3
THEN Hours1
WHEN Hours2 < Hours3
THEN Hours2
ELSE Hours3
END
) AS Lowest
FROM #t1
Many thanks for your help