Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Calculating percentages

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-04 : 15:14:12
Jim writes "I have several columns in a database which hold answers to a questionaire.

Each column/row value could be 1 through 4

Example:
          Q1
__

row1 1
row2 3
row3 4
row4 4
row5 1
row6 2


I want to be able to write a stored procedure that returns the count of each instance in column Q1 and the total rows used in the table. The output should be something like:

          1Q1     2Q1     3Q1    4Q1  RCQ1
row1 2 1 1 2 6


I will end up having several columns so if I have 5 now my output will have 25 columns of returned info.
What I ultimately want to do is caluclate the percentage of times a question is answered with a value 1 through 4.

I've searched for days on doing this and I can't find anything. Please HELP!!!!!

Running SQL 7.0 on Windows NT with SP6"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-12-04 : 15:40:28
If you don't feel like writing 25 CASE statements like this:

SELECT Count(CASE Q1 WHEN 1 THEN Q1 END) "1Q1",
Count(CASE Q1 WHEN 2 THEN Q1 END) "2Q2",
Count(CASE Q1 WHEN 3 THEN Q1 END) "3Q3",
Count(CASE Q1 WHEN 4 THEN Q1 END) "4Q4", --...etc., repeat for each Q1 value
Count(*) RCQ1
FROM myTable


...this article might help:

http://www.sqlteam.com/item.asp?ItemID=2955

Go to Top of Page
   

- Advertisement -