I'm working with survey data. Essentially, I want to count total records for a location, then use that total to create what percentage of all records each response is. I then want to group results by location.
An ideal output would be similar to this:
Question1 | State | City | %
yes | MA | bos |10
no | MA | bos |40
maybe | MA | bos |50
yes | MA | cam |20
no | MA | cam |20
maybe | MA | cam |80
The problem I run into (I believe) is that GROUP BY works before my count statement, so I can't count all the responses. Below is an example of what I have so far (not all questions have responses, so the nullif is an attempt to not count records that don't have responses to that question):
Any help or direction would be much appreciated.
An ideal output would be similar to this:
Question1 | State | City | %
yes | MA | bos |10
no | MA | bos |40
maybe | MA | bos |50
yes | MA | cam |20
no | MA | cam |20
maybe | MA | cam |80
The problem I run into (I believe) is that GROUP BY works before my count statement, so I can't count all the responses. Below is an example of what I have so far (not all questions have responses, so the nullif is an attempt to not count records that don't have responses to that question):
Code:
SELECT q1, state, city, (count(q1)*100)/(count(nullif(q1,0))) as percent FROM master group by state, city, q1
Any help or direction would be much appreciated.