参加する場所の条件:Postgresqlの句は、と私は私のクエリに特定の条件のために参加を追加しようとしています
select *
from (
select
row_number() over (partition by dl.value order by random()) as rn,
dl.value,
q.question_text, q.option_a, q.option_b, q.option_c, q.option_d,
q.correct_answer, q.image_link, q.question_type
from
questions_bank q
inner join
sports_type st on st.id = q.sports_type_id
inner join
difficulty_level dl on dl.id = q.difficulty_level_id
where st.game_type = lower('cricket') and dl.value in ('E','M','H')
) s
where
value = 'E' and rn <= 7 or
value = 'M' and rn <= 4 or
value = 'H' and rn = 1
だから、もし値=「E」、これらの質問の50%を(7) '一般的な'質問カテゴリからのものでなければなりません。
他の値(M/H)については、 "case when dl.value='E' then rn=4 (50% of 7) from question_category='general', 3 (7-4) else 7 end
"
ような何か(私はINNER JOIN question_category qc ON qc.id = q.question_category_id
のように参加を追加する必要があります)
に、いずれかがquestion_category
参照してくださいとそこに参加すべきではありません元の質問を表示するにはquestion
UPDATE:
私がやろうとしています:
select *
from (
select
row_number() over (partition by dl.value order by random()) as rn,
row_number() over (partition by dl.value, LOWER(qc.value) = LOWER('general') order by random()) as rnc,
dl.value, qc.value as question_category,
q.question_text, q.option_a, q.option_b, q.option_c, q.option_d,
q.correct_answer, q.image_link, q.question_type
from
questions_bank q
inner join
question_category qc on qc.id = q.question_category_id
inner join
sports_type st on st.id = q.sports_type_id
inner join
difficulty_level dl on dl.id = q.difficulty_level_id
where st.game_type = lower('cricket') and dl.value in ('E','M','H')
) s
where
(value = 'E' and rnc <= 4) or (value = 'E' and rn <= 3)or
value = 'M' and rn <= 3 or
value = 'H' and rn <= 2;
が、これは値= 'E' のための余分な行を返しています。 (rncから4つ、value = 'E'のときrnから4)。私は何が欠けていますか?
が、これら7で、4つの質問は、あなたが質問にそれを置くべき –
= '一般' question_categoryからでなければなりません。現時点では質問の 'question_category = 'general''については何もありません。 – joop