2017-07-18 29 views
0

を問い合わせます。は、私は次のような構造を持つテーブルと呼ばれる結果を持って

タスクテーブルに基づいて、ビューを作成し、その結果カラム(50のカットオフ値を有する)の統計を計算し、ビューに別の列にそれらを追加することです。

Iの結果のために特定の統計の計算を行い、記憶された関数作成した:

FUNCTION `calc_stat`(student INT, test INT, res INT) RETURNS float 
BEGIN 
    declare tests INT; 
    declare totaltestamount INT unsigned DEFAULT 35; 
    declare weighted_res FLOAT; 
    declare maxres INT; 

select max(result) into maxres 
from results 
where results.id_student = student; 

select count(*) into tests 
from results 
where results.id_test = test; 

set weighted_res = res/maxres*tests/totaltestamount; 

return weighted_res; 
END 

と同様にビュー:

create view `statistics` as 
    select `id_test` AS `id_test`, `id_student` AS `id_student`, `result` AS `result`,calc_stat(`id_student`, `id_test`,`result`) AS `statistics` 
    from `results` 

Iがない部分のみを、取得されるカットオフ値は、わからないので、結果が50以上のテーブルを取得する方法と、取得したビューに基づいて関数を適用するだけです。これを実行する方法はありますか?

答えて

0

あなただけのビューでこれを追加し、結果> 50で行のみを表示したい場合は、次の

create view `statistics` as 
select `id_test` AS `id_test`, `id_student` AS `id_student`, `result` AS `result`,calc_stat(`id_student`, `id_test`,`result`) AS `statistics` 
from `results` WHERE `result` > 50 

あなたはすべての行を表示するだけで> 50の結果を持つレコードの機能を適用したい場合。関数に条件を追加します。このようなもの。

FUNCTION `calc_stat`(student INT, test INT, res INT) RETURNS float 
BEGIN 
declare tests INT; 
declare totaltestamount INT unsigned DEFAULT 35; 
declare weighted_res FLOAT; 
declare maxres INT; 

select max(result) into maxres 
from results 
where results.id_student = student; 

select count(*) into tests 
from results 
where results.id_test = test; 

set weighted_res = res/maxres*tests/totaltestamount; 

if res < 50 then 
    set weighted_res = 0 // or something 
end if 

return weighted_res; 
END 
+0

明確な何かのres <50そして セットweighted_res = 0 //または何か エンド – sebas0205

+0

もしあれば。そして、私はそれを知っているが、私が望むのは> 50で結果を取得し、それが全体のテーブル(その半分ではない)であることを考慮して、検索結果に関数を適用することです。 – Cap

+0

結果が50未満の場合、ビューの「統計」列に表示する内容は? – sebas0205

関連する問題