Solved

Need to SUM the values in a row

Posted on 2009-05-05
3
559 Views
Last Modified: 2012-06-22
I've got a stored procedure which is working well, it's purpose is to check whether or not a user has enabled or disabled certain parts of a dashboard.  The fields are all tinyint using 1/0 and I'd like to be able to add up those fields to give me a total number of active items.  Here's what I've got so far:

SELECT userShowProcess, userShowLEN, userShowContractExp, userShowWO, userShowTSO, userShowOcc, userShowRev, userShowDelinq, userShowExp
FROM tblUsers
WHERE userID = @UserID;

I'd like to get another field at the end (something like profileOptionsCount) that would add up all the fields that are set to "1" like so:
userShowProcess   userShowLEN   userShowWO   userShowTSO   userShowOcc   userShowRev   userShowDelinq   userShowExp   profileOptionsCount
---------------   -----------   ----------   -----------   -----------   -----------   --------------   -----------   -------------------
1		  0		1            1             1             0             0                0	      4

Open in new window

0
Comment
Question by:saoirse1916
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 24309738
SELECT      userShowProcess,
      userShowLEN,
      userShowContractExp,
      userShowWO,
      userShowTSO,
      userShowOcc,
      userShowRev,
      userShowDelinq,
      userShowExp,
      userShowProcess + userShowLEN + userShowWO + userShowTSO +
      userShowOcc + userShowRev + userShowDelinq + userShowExp profileOptionsCount
FROM      tblUsers
WHERE      userID = @UserID;
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24309747
By the way you skipped userShowContractExp, was that intentional, otherwise:

SELECT      userShowProcess,
      userShowLEN,
      userShowContractExp,
      userShowWO,
      userShowTSO,
      userShowOcc,
      userShowRev,
      userShowDelinq,
      userShowExp,
      userShowProcess + userShowLEN + userShowContractExp + userShowWO + userShowTSO +
      userShowOcc + userShowRev + userShowDelinq + userShowExp profileOptionsCount
FROM      tblUsers
WHERE      userID = @UserID;
0
 
LVL 8

Author Closing Comment

by:saoirse1916
ID: 31578239
Perfect -- I had no idea it was that simple!  Thanks very much!
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 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