|
Posted by Erland Sommarskog on 02/16/07 08:19
Otto (ottoleholt@tele.dk) writes:
> "Erland Sommarskog" <esquel@sommarskog.se> skrev i en meddelelse >
>
> SELECT cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) / COUNT(*)
> as decimal(5,2)) FROM tbl
>
> Hi erland - thanks for your response- the code works in the way that I
> dont get an error - but I dont really know how to get the number of
> records (in Field Q1) and the % of these who have the value 'nej' out
> from this code..:-(
That code gives you the percentage who said no. COUNT(*) gives you the
the total number of rows.
SELECT COUNT(*) AS total
cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) /
COUNT(*) as decimal(5,2)) AS percent_that_said_no
FROM tbl
If Q1 can be NULL, and you only want to count non-NULL rows, replace
COUNT(*) with COUNT(Q1).
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Navigation:
[Reply to this message]
|