multi-statement table-valued function calculate/display percentages
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 a function sample below:
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
Currently this code doesn't display anything to me?
I have this to calculate percentages too, but my question is, how to get
user input? to run the functions?
No comments:
Post a Comment