?
Solved

Need Stored Procedure that adds only when matching column is true

Posted on 2011-10-12
3
Medium Priority
?
175 Views
Last Modified: 2012-05-12
Hello

I have 5 columns in a database with a value

val1
..
..
..
val5

and 5 matching bit columns

ValBit1
..
...
..
ValBit5


I need to sum up the values in val1 only when Valbit1 is true Val2 when Val2 is true etc etc

all returning 1 summed value

I need to do this is a stored procedure not sure how to proceed?

Thanks


0
Comment
Question by:Charles Baldo
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1600 total points
ID: 36960045
Whether you do this in a stored procedure or otherwise, the solution is likely the same if I understand your requirement. Just use CASE.

SELECT 
   CASE ValBit1 WHEN 1 THEN Val1 ELSE 0 END +
   CASE ValBit2 WHEN 1 THEN Val2 ELSE 0 END +
   CASE ValBit3 WHEN 1 THEN Val3 ELSE 0 END +
   CASE ValBit4 WHEN 1 THEN Val4 ELSE 0 END +
   CASE ValBit5 WHEN 1 THEN Val5 ELSE 0 END AS ValSum
FROM your_table
;

Open in new window

0
 
LVL 13

Assisted Solution

by:sameer2010
sameer2010 earned 400 total points
ID: 36960220
I think, you need to SUM these up. So, you could use:
SELECT 
   SUM(CASE ValBit1 WHEN 1 THEN Val1 ELSE 0 END +
   CASE ValBit2 WHEN 1 THEN Val2 ELSE 0 END +
   CASE ValBit3 WHEN 1 THEN Val3 ELSE 0 END +
   CASE ValBit4 WHEN 1 THEN Val4 ELSE 0 END +
   CASE ValBit5 WHEN 1 THEN Val5 ELSE 0 END) AS ValSum
FROM your_table
;

Open in new window

0
 

Author Closing Comment

by:Charles Baldo
ID: 36962029
Thank You all
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question