Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Minimum Value 3 Fields > 0 Expand / Collapse
Author
Message
Posted Wednesday, September 12, 2012 9:24 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
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!
Post #1358314
Posted Wednesday, September 12, 2012 9:53 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, August 4, 2014 10:06 PM
Points: 235, Visits: 320
If all three are zero, desired result would be zero.


Post #1358321
Posted Wednesday, September 12, 2012 9:54 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
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!
Post #1358323
Posted Thursday, September 13, 2012 2:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 7,234, Visits: 13,719
SELECT 
Id, A, B, C,
MinVal
FROM #MinAmt
CROSS 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 Shaw

For 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 Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1358396
Posted Thursday, September 13, 2012 4:55 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:10 AM
Points: 3,634, Visits: 5,283
ChrisM@Work (9/13/2012)
SELECT 
Id, A, B, C,
MinVal
FROM #MinAmt
CROSS 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!
Post #1358469
Posted Thursday, September 13, 2012 5:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 7,234, Visits: 13,719
dwain.c (9/13/2012)
ChrisM@Work (9/13/2012)
SELECT 
Id, A, B, C,
MinVal
FROM #MinAmt
CROSS 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 Shaw

For 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 Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1358476
Posted Thursday, September 13, 2012 6:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 8, 2014 8:15 AM
Points: 69, Visits: 293
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 ,#MinAmt
union
select a.id, a.c from #MinAmt a ,#MinAmt
) a where a>0 group by Id
Post #1358512
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse