I have a table
UserName Question_ID Answer
Tom Q001 D
Wendy Q009 A
Eddy Q089 C
David Q001 C
Eve Q001 D
Paul Q001 A
Sam Q001 B
Tom Q002 B
Tom Q003 C
I want to create multi statement tabled valued function.
Let Question_id as input, I want to create table shows question_id, answer, number of answers, and percentages of answers
For example (input: Question_id = Q001)
The output will be
Question_ID Answer Total Percentage
Q001 A 1 20
Q001 B 1 20
Q001 C 1 20
Q001 D 2 40
I have created function
create function [dbo].[QuestionFrequency]
(
@question_id varchar(10)
)
Returns @frequency table (question_id varchar(10), answer varchar(10))
As
begin
insert @frequency (question_id, answer)
select question_Id, Answer from questions where @question_id = Question_id
return
end
it shows me the out put
Question_ID Answer
Q001 D
Q001 C
Q001 D
Q001 A
Q001 B
What should i do to count the total and percentages?