SQL Select Case Sum

I have a table whose records contain at least 3 fields... Field1, Field2 and Field3.

I'd like to loop through the table and add up all the instances when the above 3 fields are positive values.

Example... if Field1 and Field2 contain a 5 and Field3 contains a 0 in all 7 records the total would be 14.

My current statement:

SELECT sum(
case

when Field1 > 0 then 1 
when Field2 > 0 then 1 
when Field3 > 0 then 1 

end)

Open in new window


My current statement returns 1 if any field is positive... thus the 7 records above would return 7 instead of 14.

Thank you for the help.
Sheldon LivingstonConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee SavidgeCommented:
Just a thought, but try this:

SELECT sum(
case

when Field1 > 0 then Field1
when Field2 > 0 then Field2
when Field3 > 0 then Field3

end)

Open in new window

0
PortletPaulEE Topic AdvisorCommented:
I think your description requires::

SELECT
          sum(case when Field1 > 0 then 1 else 0 end)
      +  sum(case when Field2 > 0 then 1 else 0 end)
      +  sum(case when Field3 > 0 then 1 else 0 end)
FROM ...

so where Field1 > 0 and Field2 is > 0 and Field3 = 0 that would be 2 per row, for 7 rows = 14
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sheldon LivingstonConsultantAuthor Commented:
lsavidge... that summed up the values... thus, using the 7 records above, it returned 35.

BTW... the fields will contain a variety of values... not just 5.
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Sheldon LivingstonConsultantAuthor Commented:
Sweet!  Thank you PortletPaul... this worked.
0
PortletPaulEE Topic AdvisorCommented:
No problem. Cheers, Paul
0
Lee SavidgeCommented:
Ahh, nice. I read the question differently!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.