Solved

Need to SUM the values in a row

Posted on 2009-05-05
3
563 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 Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

627 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