sql - 从每个类别中选择n个样本

标签 sql sqlite

我有一列,分数,它是介于1和5之间(含1和5)的整数。
我试图从每个分数中选择n个(在这种情况下为2000个)样本。
我自己的黑客攻击和其他SO问题导致我构造了以下查询:

select * from (select text, score from data where score= 1 and LENGTH(text) > 45 limit 2000)
union
select * from (select text, score from data where score= 2 and LENGTH(text) > 45 limit 2000)
union
select * from (select text, score from data where score= 3 and LENGTH(text) > 45 limit 2000)
union
select * from (select text, score from data where score= 4 and LENGTH(text) > 45 limit 2000)
union
select * from (select text, score from data where score= 5 and LENGTH(text) > 45 limit 2000)


感觉这是最糟糕的方式,更重要的是,当我分别运行每个查询时,它给我2k个结果,但是,当我运行此联合时,我得到的行数不到1万行
我正在寻找帮助来优化此查询,但是更重要的是我想了解为什么工会返回错误的结果数

最佳答案

关于查询为什么返回错误数量的结果的原因,我敢打赌您的数据不在每个查询返回的结果集中的distinct内。使用union时,它将返回整个结果集中的distinct行。

尝试将其更改为union all

select * from (select text, score from data where score= 1 and LENGTH(text) > 45 limit 2000)
union all
select * from (select text, score from data where score= 2 and LENGTH(text) > 45 limit 2000)
union all
select * from (select text, score from data where score= 3 and LENGTH(text) > 45 limit 2000)
union all
select * from (select text, score from data where score= 4 and LENGTH(text) > 45 limit 2000)
union all
select * from (select text, score from data where score= 5 and LENGTH(text) > 45 limit 2000)



Here's a condensed demo showing the difference.




如果您具有主键(例如自动增量),那么这是另一种方法,它为每组分数生成一个row_number(这假设一个id主键):

select text, score
from (
  select text, score, 
         (select count(*) from data b 
          where a.id >= b.id and 
                a.score = b.score and 
                length(b.text) > 45) rn
  from data a
  where length(text) > 45
  ) t
where rn <= 2000

关于sql - 从每个类别中选择n个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50803974/

相关文章:

android - 在 Android SQLite 数据库中存储值的最快方法?

sql - 有没有办法使一列的可为空性取决于另一列的可为空性?

mysql - 如何避免主键错误

mysql - 有什么技巧可以使查询在满足某些条件时不输出行而在不满足条件时输出行(没有子选择)?

MySQL str_to_date 问题

android - 表更新时如何将本地 SQLite 数据库同步到服务器

c - 将sqlite3 db数据读入uint64_t

android - SQLite 数据库是否计入应用程序的内存限制?

sqlite - Firefox中的WebSQL

mysql - 创建 ID 介于 1-999999 之间的数据库