Mike
Mike

Reputation: 567

Sumarising a table

Using SQL Server I'm trying to summarize a questionnaire table as follows but am struggling!

I want to show the % of all Questionnaires answered true by month and year.

enter image description here

Can anyone help?

Upvotes: 0

Views: 38

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269553

You can do this with conditional aggregation. This method shows conditional use of avg():

select "Year", "Month",
       avg(case when recommend = 'true' then 1.0 else 0.0 end) * 100 as "True %"
from Questionnaire q
group by "Year", "Month"
order by "Year", "Month";

If you actually want the "%" at the end, you need to convert the result to a string and append it.

Upvotes: 1

Related Questions