March 8, 2016 at 1:30 pm
I have 4 different cost fields....I'd like to get rid of the records that have 0.00 in all 4.
Example:
ID Cost1 Cost2 Cost3 Cost4
1122 14.00 0.00 50.00 25.00
1133 75.00 32.00 1.00 0.00
1144 0.00 0.00 0.00 0.00
I'd like to get rid of the record for ID 1144
BUT
when I do something like:
where cost1 <> '0.00'
and cost2 <> '0.00'
and cost3 <> '0.00'
and cost4 <> '0.00'
i eliminates all 3 records. Probably because the 1st two records have '0.00' in one of the cost fields. Is there a way to say to get rid of records that has all four fields with 0.00?
March 8, 2016 at 1:39 pm
Just need to tweak your logic...
DECLARE @myTable TABLE (ID INT, Cost1 NUMERIC(4,2), Cost2 NUMERIC(4,2), Cost3 NUMERIC(4,2), Cost4 NUMERIC(4,2))
INSERT INTO @myTable
VALUES (1122, 14.00, 0.00, 50.00, 25.00), (1133, 75.00, 32.00, 1.00, 0.00), (1144, 0.00, 0.00, 0.00, 0.00)
SELECT * FROM @myTable
SELECT *
FROM @myTable
WHERE Cost1 <> 0.00 OR Cost2 <> 0.00 OR Cost3 <> 0.00 OR Cost4 <> 0.00
What is happening is that as soon as any of the predicates return true you return the row. When they are all zero none of them are true and therefore not returned by the SELECT statement.
March 8, 2016 at 1:44 pm
That worked....thanks!
March 8, 2016 at 1:48 pm
cory.bullard76 (3/8/2016)
That worked....thanks!
You're welcome!
March 8, 2016 at 3:27 pm
i've done something similar, but i just add the items together, and test for greater than zero; my query is assuming cost is a positive value, never negative, is that a safe assumption?
WHERE Cost1 + Cost2 + Cost3 + Cost4 >0
Lowell
March 9, 2016 at 11:03 am
as lowel says, only use his if you only have positive values, you could do
ABS(Cost1) + ABS(Cost2) + ABS(Cost3) + ABS(Cost4) >0
This will protect against negative numbers.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply