2017-02-10 1 views
0

ich einen Join für einen bestimmten Zustand zu meiner Anfrage hinzufügen versuchen:Postgresql - Zustand auf, wo Klausel mit beitreten

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 

Also, wenn value = 'E', 50% dieser Fragen (7) sollte von einer "allgemeinen" Fragekategorie sein.

Etwas wie "case when dl.value='E' then rn=4 (50% of 7) from question_category='general', 3 (7-4) else 7 end"

(ich brauche, um eine wie INNER JOIN question_category qc ON qc.id = q.question_category_id beitreten hinzufügen)

Für andere Werte (M/H), sollte es nicht mit question_category beitreten sein

Siehe question, um die ursprüngliche Frage zu sehen.

UPDATE:

Ich versuche zu tun:

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; 

aber kehrt zusätzliche Zeilen für value = 'E'. (4 von rnc und 4 von rn, wenn Wert = 'E'). Was vermisse ich?

Antwort

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

Wenn dl.value = 'E' sollte rn 7 nur, aber in diesen 7 sollten vier Fragen aus question_category sein = 'allgemeine' –

+0

Sie, dass in die Frage stellen sollte. Im Moment gibt es nichts über 'fragment_category = 'general' in der Frage. – joop

0

Ich gab die general Kategorie die 1 id. Bei Bedarf ersetzen.

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

Die Gesamtfragen sollten nur 12 sein. aber die Nr. Fragen in E müssen zu 50% (von E) aus der allgemeinen Kategorie bestehen. –

+0

@AnkitaGupta 50% von 7 ist 3,5. Was ist zu tun? –

+0

würde es zu einem int gerundet werden, so, 4. –

0

ich die Art und Weise herausfinden konnte,

finden, was ich mit aufkam.

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