Выберите случайные значения для сгруппированного набора данных - PullRequest
0 голосов
/ 14 октября 2019

Я не одарён SQL. Однако я использую следующий запрос:

select count(*) as countis, avclassfamily
from malwarehashesandstrings
where behaviouralbinary IS true and
       avclassfamily != 'SINGLETON'
group by avclassfamily
ORDER BY countis desc
LIMIT 50;

Я бы хотел выбрать 3 случайных хеша из столбца malwarehashsha256, сгруппированных по столбцу avclassfamily.

Следующий запрос работает, вопрос закончен:

select count(*) as countis,avclassfamily from malwarehashesandstrings where behaviouralbinary IS true and avclassfamily != 'SINGLETON' group by avclassfamily ORDER BY countis desc LIMIT 50;

virustotal=# select m.avclassfamily, m.cnt,
        array_agg(malwarehashsha256)
 from (select malwarehashesandstrings.*,
              count(*) over (partition by avclassfamily) as cnt,
              row_number() over (partition by avclassfamily order by random()) as seqnum
       from malwarehashesandstrings
       where behaviouralbinary and
             avclassfamily <> 'SINGLETON'
      ) as m
 where seqnum <= 3
 group by m.avclassfamily, m.cnt ORDER BY m.cnt DESC LIMIT 50;

1 Ответ

1 голос
/ 14 октября 2019

Если я правильно понимаю, вы можете использовать row_number():

select m.*
from (select m.*,
             row_number() over (partition by m.avclassfamily order by random()) as seqnum
      from malwarehashesandstrings m
      where m.behaviouralbinary and
            m.avclassfamily <> 'SINGLETON'
     ) m
where seqnum <= 3;

Если вы хотите это в столбце в вашем существующем запросе, один из методов:

select m.avgclassfamily, m.cnt,
       array_agg(m.malwarehashsha256)
from (select m.*,
             count(*) over (partition by m.avgclassfamily) as cnt,
             row_number() over (partition by m.avclassfamily order by random()) as seqnum
      from malwarehashesandstrings m
      where m.behaviouralbinary and
            m.avclassfamily <> 'SINGLETON'
     ) m
where seqnum <= 3
group by m.avgclassfamily, m.cnt;
...