Re: SQL Query Problem



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.


.



Relevant Pages

  • Re: oh my god
    ... <melvinjxyz@xxxxxxxxxxxx(remove xyz to get me)> ... at least trim his message and don't repost his links. ... Microsoft MVP Windows - Shell/User ... Please Reply to the Newsgroup ...
    (microsoft.public.windowsxp.general)
  • Re: What is MS doing about the halting mouse problem?
    ... There are several threads on this newsgroup where other people are having ... More than a coincidense I'd say old bean! ... > xyz wrote: ...
    (microsoft.public.windowsxp.general)