Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL 2008 Count Non 0 rows

Posted on 2011-02-15
3
Medium Priority
?
411 Views
Last Modified: 2012-05-11
Hi,

I have a table with a int field.  Without using a sub query I'd like to count the number of rows based on a condition, i.e. count the number of rows where a column is not 0.  i.e.
SELECT *, (COUNT(* WHERE COL A IS NOT 0)) FROM tablename.
0
Comment
Question by:kinton
3 Comments
 
LVL 15

Accepted Solution

by:
tim_cs earned 2000 total points
ID: 34896746
You coudl just do a SUM with a case statement.

SELECT SUM (WHEN COL <> 0 THEN 1 ELSE 0 END )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34896759
> Without using a sub query
you cannot do that at the same level as the query itself ...
0
 
LVL 2

Author Closing Comment

by:kinton
ID: 34897022
Great, good answer!  Should have thought of that..
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

783 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