## Minimum Value 3 Fields > 0

 Author Message dwain.c SSC-Forever Group: General Forum Members Points: 44671 Visits: 6431 Ah yes, NULLIF... Forgot about that one.That will work but we still need to hear from the OP their preferred return result for that case so it can be plugged into your version of my query. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables texpic Hall of Fame Group: General Forum Members Points: 3158 Visits: 463 If all three are zero, desired result would be zero. dwain.c SSC-Forever Group: General Forum Members Points: 44671 Visits: 6431 texpic (9/12/2012)If all three are zero, desired result would be zero.What if all 3 are negative? Still zero? My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables ChrisM@Work SSC Guru Group: General Forum Members Points: 97845 Visits: 20721 `SELECT Id, A, B, C, MinVal FROM #MinAmtCROSS APPLY ( SELECT MIN(a) FROM (VALUES (A),(B),(C) ) v (a) WHERE a > 0) x (MinVal) `;-) “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps dwain.c SSC-Forever Group: General Forum Members Points: 44671 Visits: 6431 ChrisM@Work (9/13/2012)`SELECT Id, A, B, C, MinVal FROM #MinAmtCROSS APPLY ( SELECT MIN(a) FROM (VALUES (A),(B),(C) ) v (a) WHERE a > 0) x (MinVal) `;-)And the referee throws down a red penalty flag!This is the SQL 2005 forum! My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables ChrisM@Work SSC Guru Group: General Forum Members Points: 97845 Visits: 20721 dwain.c (9/13/2012)ChrisM@Work (9/13/2012)`SELECT Id, A, B, C, MinVal FROM #MinAmtCROSS APPLY ( SELECT MIN(a) FROM (VALUES (A),(B),(C) ) v (a) WHERE a > 0) x (MinVal) `;-)And the referee throws down a red penalty flag!This is the SQL 2005 forum!Challenged! texpic always posts in the 2K8 forum! “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps raghava_tg SSC-Addicted Group: General Forum Members Points: 412 Visits: 317 Try This:-)select Id,MIN(a) from (select a.id, a.a from #MinAmt a ,#MinAmt union select a.id, a.b from #MinAmt a ,#MinAmtunion select a.id, a.c from #MinAmt a ,#MinAmt) a where a>0 group by Id