列随机值不同

时间:2017-10-04 10:21:56

标签: mysql postgresql distinct-on

我需要从我的桌子上提取12个问题。我有一个名为" bucket"可能有重复的值。 在获取时,我只需要获取唯一的桶值,行数必须为12。

这是我的疑问:

select *
from (
    select
        DISTINCT ON (q.bucket) bucket,
        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 <= 3 and LOWER(question_category) != LOWER('general')) or
    (value = 'E' and rnc <= 3 and LOWER(question_category) = LOWER('general')) or
    value = 'M' and rn <= 4 or
    value = 'H' and rn <= 2;

任何人都可以告诉我这里我做错了什么?它有时不会返回12行。只要找到相同的桶值,就会发生这种情况。我认为,只要应用了distinct,就会删除重复行的值。因此,当我做rn&lt; = 4时,它无法找到说,3。因此只返回3行而不是4行。

所以,我需要首先在bucket上应用distinct,然后获取row_numbers。我该怎么做?

0 个答案:

没有答案