Solved

MS SQL 2008 Count Non 0 rows

Posted on 2011-02-15
3
395 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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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