﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Minimum Value 3 Fields &amp;gt; 0 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 09:36:53 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>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&amp;gt;0 group by Id</description><pubDate>Thu, 13 Sep 2012 06:33:01 GMT</pubDate><dc:creator>raghavatgk</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]dwain.c (9/13/2012)[/b][hr][quote][b]ChrisM@Work (9/13/2012)[/b][hr][code="sql"]SELECT 	Id, A, B, C, 	MinVal FROM #MinAmtCROSS APPLY (	SELECT MIN(a) 	FROM (VALUES (A),(B),(C) ) v (a) 	WHERE a &amp;gt; 0) x (MinVal) [/code];-)[/quote]And the referee throws down a red penalty flag!This is the SQL 2005 forum![/quote]Challenged! texpic always posts in the 2K8 forum!</description><pubDate>Thu, 13 Sep 2012 05:11:17 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]ChrisM@Work (9/13/2012)[/b][hr][code="sql"]SELECT 	Id, A, B, C, 	MinVal FROM #MinAmtCROSS APPLY (	SELECT MIN(a) 	FROM (VALUES (A),(B),(C) ) v (a) 	WHERE a &amp;gt; 0) x (MinVal) [/code];-)[/quote]And the referee throws down a red penalty flag!This is the SQL 2005 forum!</description><pubDate>Thu, 13 Sep 2012 04:55:38 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[code="sql"]SELECT 	Id, A, B, C, 	MinVal FROM #MinAmtCROSS APPLY (	SELECT MIN(a) 	FROM (VALUES (A),(B),(C) ) v (a) 	WHERE a &amp;gt; 0) x (MinVal) [/code];-)</description><pubDate>Thu, 13 Sep 2012 02:31:22 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]texpic (9/12/2012)[/b][hr]If all three are zero, desired result would be zero.[/quote]What if all 3 are negative?  Still zero?</description><pubDate>Wed, 12 Sep 2012 21:54:56 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>If all three are zero, desired result would be zero.</description><pubDate>Wed, 12 Sep 2012 21:53:08 GMT</pubDate><dc:creator>texpic</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>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.</description><pubDate>Wed, 12 Sep 2012 21:24:50 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]dwain.c (9/12/2012)[/b][hr][quote][b]Michael Valentine Jones (9/12/2012)[/b][hr][quote][b]dwain.c (9/12/2012)[/b][hr]Another way:[code="sql"];WITH MyValues AS (    SELECT Id         ,A=CASE WHEN A &amp;gt; 0 THEN A ELSE 2147483647 END        ,B=CASE WHEN B &amp;gt; 0 THEN B ELSE 2147483647 END        ,C=CASE WHEN C &amp;gt; 0 THEN C ELSE 2147483647 END    FROM #MinAmt)SELECT Id    ,MinAmt=CASE         WHEN A &amp;lt; B THEN CASE WHEN A &amp;lt; C THEN A ELSE C END        WHEN B &amp;lt; A THEN CASE WHEN B &amp;lt; C THEN B ELSE C END        ELSE CASE WHEN C &amp;lt; A THEN C ELSE A END  ENDFROM MyValues[/code]BTW.  You didn't specify what to return if all values are &amp;lt;= 0.[/quote]You also need to deal with the case where the values are null.You code returns 2147483647 when they are all &amp;lt;1 or null, and I doubt that is what they would want.Returning a null probably makes the most sense for that case.[/quote]That's why I asked what the OP wanted for that case.  Easy enough to wrap another CASE around the whole result like:[code="sql"]CASE WHEN [longer case] IS 2147483647 THEN NULL ELSE [longer case] END[/code]Or if that seems too messy, put my CASE into a CROSS APPLY and just return that result to this latest CASE.[/quote]Wrapping it in NULLIF([longer case],2147483647) would be simpler.Of course, if one of the values in the table was actually 2147483647, it would return a null, so you might need extra code to deal with that:[code="sql"]WITH MyValues AS (    SELECT Id         ,A=CASE WHEN A &amp;gt; 0 THEN A ELSE 3000000000000 END        ,B=CASE WHEN B &amp;gt; 0 THEN B ELSE 3000000000000 END        ,C=CASE WHEN C &amp;gt; 0 THEN C ELSE 3000000000000 END    FROM #MinAmt)SELECT Id    ,MinAmt=	convert(int,nullif(	CASE         WHEN A &amp;lt; B THEN CASE WHEN A &amp;lt; C THEN A ELSE C END        WHEN B &amp;lt; A THEN CASE WHEN B &amp;lt; C THEN B ELSE C END        ELSE CASE WHEN C &amp;lt; A THEN C ELSE A END  END        ,3000000000000))FROM MyValues[/code]</description><pubDate>Wed, 12 Sep 2012 21:20:31 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]Michael Valentine Jones (9/12/2012)[/b][hr][quote][b]dwain.c (9/12/2012)[/b][hr]Another way:[code="sql"];WITH MyValues AS (    SELECT Id         ,A=CASE WHEN A &amp;gt; 0 THEN A ELSE 2147483647 END        ,B=CASE WHEN B &amp;gt; 0 THEN B ELSE 2147483647 END        ,C=CASE WHEN C &amp;gt; 0 THEN C ELSE 2147483647 END    FROM #MinAmt)SELECT Id    ,MinAmt=CASE         WHEN A &amp;lt; B THEN CASE WHEN A &amp;lt; C THEN A ELSE C END        WHEN B &amp;lt; A THEN CASE WHEN B &amp;lt; C THEN B ELSE C END        ELSE CASE WHEN C &amp;lt; A THEN C ELSE A END  ENDFROM MyValues[/code]BTW.  You didn't specify what to return if all values are &amp;lt;= 0.[/quote]You also need to deal with the case where the values are null.You code returns 2147483647 when they are all &amp;lt;1 or null, and I doubt that is what they would want.Returning a null probably makes the most sense for that case.[/quote]That's why I asked what the OP wanted for that case.  Easy enough to wrap another CASE around the whole result like:[code="sql"]CASE WHEN [longer case] IS 2147483647 THEN NULL ELSE [longer case] END[/code]Or if that seems too messy, put my CASE into a CROSS APPLY and just return that result to this latest CASE.</description><pubDate>Wed, 12 Sep 2012 20:57:44 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]dwain.c (9/12/2012)[/b][hr]Another way:[code="sql"];WITH MyValues AS (    SELECT Id         ,A=CASE WHEN A &amp;gt; 0 THEN A ELSE 2147483647 END        ,B=CASE WHEN B &amp;gt; 0 THEN B ELSE 2147483647 END        ,C=CASE WHEN C &amp;gt; 0 THEN C ELSE 2147483647 END    FROM #MinAmt)SELECT Id    ,MinAmt=CASE         WHEN A &amp;lt; B THEN CASE WHEN A &amp;lt; C THEN A ELSE C END        WHEN B &amp;lt; A THEN CASE WHEN B &amp;lt; C THEN B ELSE C END        ELSE CASE WHEN C &amp;lt; A THEN C ELSE A END  ENDFROM MyValues[/code]BTW.  You didn't specify what to return if all values are &amp;lt;= 0.[/quote]You also need to deal with the case where the values are null.You code returns 2147483647 when they are all &amp;lt;1 or null, and I doubt that is what they would want.Returning a null probably makes the most sense for that case.</description><pubDate>Wed, 12 Sep 2012 20:53:36 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>Another way:[code="sql"];WITH MyValues AS (    SELECT Id         ,A=CASE WHEN A &amp;gt; 0 THEN A ELSE 2147483647 END        ,B=CASE WHEN B &amp;gt; 0 THEN B ELSE 2147483647 END        ,C=CASE WHEN C &amp;gt; 0 THEN C ELSE 2147483647 END    FROM #MinAmt)SELECT Id    ,MinAmt=CASE         WHEN A &amp;lt; B THEN CASE WHEN A &amp;lt; C THEN A ELSE C END        WHEN B &amp;lt; A THEN CASE WHEN B &amp;lt; C THEN B ELSE C END        ELSE CASE WHEN C &amp;lt; A THEN C ELSE A END  ENDFROM MyValues[/code]BTW.  You didn't specify what to return if all values are &amp;lt;= 0.</description><pubDate>Wed, 12 Sep 2012 19:11:33 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[quote][b]sogou34 (9/10/2012)[/b][hr]--------  http://www.chic-goods.us/ -----------hello,This is a wholesaler's web site.---blah blah[/quote]Reported.</description><pubDate>Tue, 11 Sep 2012 01:39:21 GMT</pubDate><dc:creator>Phil Parkin</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>SS thanks</description><pubDate>Mon, 10 Sep 2012 20:22:25 GMT</pubDate><dc:creator>texpic</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>[code="sql"]select	ID,	[MinVal] = (		select			min(x)		from			(			select x=A where A &amp;gt; 0 union all			select x=B where B &amp;gt; 0 union all			select x=C where C &amp;gt; 0			) m		)from	#MinAmtorder by	ID[/code]Results:[code="plain"]ID          MinVal----------- -----------1           12           43           24           15           26           1(6 row(s) affected)[/code]</description><pubDate>Mon, 10 Sep 2012 20:19:39 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>Yes 2, sorry</description><pubDate>Mon, 10 Sep 2012 19:53:34 GMT</pubDate><dc:creator>texpic</dc:creator></item><item><title>RE: Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>For ID = 3, the answer should be 2 not 3, right?</description><pubDate>Mon, 10 Sep 2012 19:48:20 GMT</pubDate><dc:creator>ColdCoffee</dc:creator></item><item><title>Minimum Value 3 Fields &amp;gt; 0</title><link>http://www.sqlservercentral.com/Forums/Topic1357088-338-1.aspx</link><description>This seems like it should be easy but I can't seem to come up with a simple formula.  Need the minimum value where answer &amp;gt; 0.Result:1 = 12 = 43 = 34 = 15 = 26 = 1[code="sql"]CREATE TABLE #MinAmt(Id int, A int, B int, C int)INSERT INTO #MinAmt(Id,A,B,C)SELECT 1,1,2,3 UNION ALLSELECT 2,4,5,6 UNION ALLSELECT 3,0,2,3 UNION ALLSELECT 4,1,2,0 UNION ALLSELECT 5,-1,2,3 UNION ALLSELECT 6,1,2,-3 [/code]</description><pubDate>Mon, 10 Sep 2012 19:41:25 GMT</pubDate><dc:creator>texpic</dc:creator></item></channel></rss>