如何限制SQL中每个组的大小?

时间:2016-04-11 20:07:20

标签: sql

E.g。我有一张看起来像这样的表:

(ID, TEXT, CLASS)
(0L, "a b c d e spark", 1.0),
(1L, "b d", 0.0),
(2L, "spark f g h", 1.0),
(3L, "hadoop mapreduce", 0.0),
(3L, "hadoop mapreduce", 1.0),
(4L, "b spark who", 1.0),
(5L, "g d a y", 0.0),
(6L, "spark fly", 1.0),
(7L, "was mapreduce", 0.0),
(8L, "e spark program", 1.0),
(9L, "a e c l", 0.0),
(10L, "spark compile", 1.0),
(11L, "hadoop software", 0.0)

我想选择一个将每个类的大小限制为2的表,而只保留ID最小的行,是否有可以执行此操作的SQL命令?

1 个答案:

答案 0 :(得分:1)

ANSI标准方法是使用row_number()

select t.*
from (select t.*,
             row_number() over (partition by class order by id) as seqnum
      from t
     ) t
where seqnum <= 2;

大多数(但不是全部)数据库都支持此功能。