Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a table that stores survey results in 10 columns. Each column represents a question, with the answer being represented in a number between 0-5 (0= no response, and 5= poor). There are only 50 rows of data at this time, however this number will change. I want to count how many times a particular number (0-5) appears in each column. See below: Question1 Question 2 Question 3 ...0 3 1 01 15 9 5 2 20 18 153 10 16 124 2 2 3 5 0 4 15I can't figure out how to do something like this... any suggestions?MBeal
drymchaser
Aged Yak Warrior
552 Posts
Posted - 2003-08-11 : 12:23:43
I think you can use this or something like it:
SELECT 'Question 1' as QuestionNum , Question1 'AnswerType' , count(*) 'AnswerCount'FROM myTableGROUP BY Question1UNION ALLSELECT 'Question2', Question2, count(*)FROM myTableGROUP BY Question2UNION ALL...etc...
setbasedisthetruepath
Used SQL Salesman
992 Posts
Posted - 2003-08-11 : 12:26:28
The best answer is to change your schema so that each response is its own row, not column.Jonathan{0}
MBeal
Posting Yak Master
110 Posts
Posted - 2003-08-11 : 13:07:53
Jonathan,You mentioned changing the schema. I agree that the schema is not ideal. However, the columns represent the questions. How would you suggest changing that?MBeal
Amethystium
Aged Yak Warrior
701 Posts
Posted - 2003-08-11 : 13:17:12
I think Jonathan meant that instead of having 10 columns for 10 questions perhaps you should have it like this :QuestionID Answer1 042 013 05...etc------------------------------------------------------------------------------I enjoy using SQL Server but I am not part of the Microsoft fanboy club! NEVER!!