2017-02-10 33 views
0

我想添加特定條件,我的查詢聯接:PostgreSQL的 - 條件在where子句中使用聯接

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)應該來自「一般」問題類別。

像 「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聯接)

對於其他值(M/H),不應該有與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'。 (當值='E'時,來自rnc的4個和來自rn的4個)。我錯過了什麼?

回答

0
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 
     JOIN sports_type st ON st.id = q.sports_type_id 
     JOIN difficulty_level dl ON dl.id = q.difficulty_level_id 
    WHERE st.game_type = lower('cricket') AND dl.value IN ('E','M','H') 
    AND (dl.value = 'E' -- No extra condition for 'E' 
     OR EXISTS   -- Extra condition for non-'E' 
      (SELECT * FROM question_category qc 
      WHERE qc.id = q.question_category_id 
     ) 
     ) 
) s 
WHERE value = 'E' AND rn <= 7 
    or value = 'M' AND rn <= 4 
    or value = 'H' AND rn = 1 
     ; 
+0

當dl.value = 'E',RN應該是7只,但在這7,4個問題應該是從question_category = '一般' –

+0

你應該把該進入正題。目前在問題中沒有關於'question_category ='general''的任何信息。 – joop

0

我給了general類別1 id。根據需要更換。

select * 
from (
    select 
     row_number() over (partition by dl.value order by random()) as rn, 
     row_number() over (
      partition by dl.value, question_category_id = 1 
      order by random() 
     ) as rnc, 
     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 
     inner join 
     question_category qc on qc.id = q.question_category_id 
    where st.game_type = lower('cricket') and dl.value in ('E','M','H') 
) s 
where 
    value = 'E' and rn <= 4 or 
    value = 'M' and rnc <= 4 or 
    value = 'H' and rn = 1 
+0

總共只有12個問題。但沒有。 E中的問題需要包含一般類別的50%(E)。 –

+0

@AnkitaGupta 7的50%是3.5。該怎麼辦? –

+0

它會四捨五入到一個整數,所以,4。 –

0

我能想出的辦法,

以下是我想出了。

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, 
         row_number() over (partition by dl.value, LOWER(qc.value) != LOWER('general') order by random()) as rnq, 
         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 rnq <= 4 and LOWER(question_category) != LOWER('general')) or 
        (value = 'E' and rnc <= 3 and LOWER(question_category) = LOWER('general')) or 
        value = 'M' and rn <= 3 or 
        value = 'H' and rn <= 2