Re: SQL Query Problem
- From: "Bob Barrows [MVP]" <reb01501@xxxxxxxxxxxxxxx>
- Date: Tue, 20 Mar 2007 10:35:07 -0400
John wrote:
Hello, I am trying to find a Query in a MS SQL Server that solves
this problem: I have a column called 'xyz' (True/False). Now I want
to get the percentage
of the rows that are True.
Thanks for your help
John
select 100*sum(xyz)/count(*) as PercentTrue
from table where xyz is not null
In sql server, what you are calling "True/False" is actually a "bit"
datatype. The bit contains either 1 (true), 0 (false) or Null. So, the
above query excludes the rows where the column contains null and sums up
the contents of xyz, dividing by the total count of rows where xyz is
not null. If you want to include the null, then use coalesce to use zero
if the column contains null:
select 100*sum(coalesce(xyz,0))/count(*) as PercentTrue
from table
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
.
- Prev by Date: Re: ADO high virtual memory usage
- Next by Date: Re: ADO high virtual memory usage
- Previous by thread: Re: ADO high virtual memory usage
- Next by thread: Re: Just another SQL Query
- Index(es):
Relevant Pages
|
|