私はテーブルを持っています
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
複数ステートメントのテーブル値関数を作成したい。
Question_id を入力として、question_id、回答、回答数、および回答のパーセンテージを示すテーブルを作成したい
例 (入力: Question_id = Q001)
出力は次のようになります。
Question_ID Answer Total Percentage
Q001 A 1 20
Q001 B 1 20
Q001 C 1 20
Q001 D 2 40
以下の関数サンプルを作成しました。
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
現在、このコードは何も表示しませんか?
私もパーセンテージを計算するためにこれを持っていますが、私の質問は、ユーザー入力を取得する方法ですか? 機能を実行するには?
これはコードです:
create table [online_questionaire] ( username nchar(10) null ,
question_id nchar(20) null , answer nchar(20) null , )
online_questionaire (username, question_id, answer) 値
('tom', 'q001' ,'d'), ('wendy', 'q09' ,'a'), ('eddy', 'q089' ,'c '), ('david', 'q001' ,'c'), ('eve', 'q001' ,'d'), ('paul', 'q001' ,'a'), ('sam', 'q001' ,'b'); 行く
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 online_questionaire where @question_id = Question_id
return
end
go select q.Question_ID, q.Answer, count( ) as Total, count( ) * 100 / (select count(*) from online_questionaire as t where t.Question_ID = @question_id) as [Percentage] from online_questionaire as q where q .Question_ID= @question_id q.Question_ID, q.Answerによるグループ化
DECLARE @Question_ID VARCHAR(64) = 'Q001'
SELECT * FROM QuestionFrequency(@Question_ID) AS qf