[英]Oracle SQL: Select at least the first n rows, continue until a column value is different from the last one
给出具有以下结构的表foo(Oracle 11g):
ID | GROUP_ID
1 | 100
2 | 100
3 | 100
4 | 200
5 | 300
6 | 300
7 | 400
我想选择前n行(按ID排序)或更多,这样我总能得到一个完整的组。
例:
n = 2:我想至少获得前两行,但是由于ID 3也属于组100,所以我也希望得到它。
n = 4:给我前四行,我很高兴;-)
n = 5:请求1-6行。
非常感谢您的帮助!
使用rank()
解决方案:
select id, group_id
from (select t.*, rank() over (order by group_id) as rnk
from t)
where rnk <= :n;
建筑测试数据:
SQL> create table t (id number not null primary key
2 , group_id number not null);
Table created.
SQL> insert into t values (1, 100);
1 row created.
SQL> insert into t values (2, 100);
1 row created.
SQL> insert into t values (3, 100);
1 row created.
SQL> insert into t values (4, 200);
1 row created.
SQL> insert into t values (5, 300);
1 row created.
SQL> insert into t values (6, 300);
1 row created.
SQL> insert into t values (7, 400);
1 row created.
SQL> commit;
Commit complete.
SQL>
正在运行...
SQL> var n number
SQL> exec :n := 2;
PL/SQL procedure successfully completed.
SQL> select id, group_id
2 from (select t.*, rank() over (order by group_id) as rnk
3 from t)
4 where rnk <= :n;
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
SQL> exec :n := 4;
PL/SQL procedure successfully completed.
SQL> select id, group_id
2 from (select t.*, rank() over (order by group_id) as rnk
3 from t)
4 where rnk <= :n;
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
4 200
SQL> exec :n := 5;
PL/SQL procedure successfully completed.
SQL> select id, group_id
2 from (select t.*, rank() over (order by group_id) as rnk
3 from t)
4 where rnk <= :n;
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
4 200
5 300
6 300
6 rows selected.
编辑这是包含for update
子句(:n = 2)的版本:
SQL> select id, group_id
2 from T
3 where rowid in (select RID
4 from (select t.rowid as RID, t.*, rank() over (order by group_id) as rnk
5 from t)
6 where rnk <= :n)
7 for update;
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
如果您的ID
始终从1开始是连续的(无间隔),并且您的Group_ID
从未在其他地方作为第二个组出现。 如果您的Group_ID
的价值始终在上升...
SELECT
*
FROM
foo
WHERE
Group_ID <= (SELECT Group_ID FROM foo WHERE ID = n)
ORDER BY
ID
您将从ID
和Group_ID
上具有单独的索引中Group_ID
如果始终确实GROUP_ID
是连续且递增的,那么可以使用SQL的解析ROW_NUMBER()
函数轻松解决此问题:
SQL> select id
2 , group_id
3 from foo
4 where group_id <= ( select group_id
5 from (
6 select f.group_id
7 , row_number() over (order by f.id asc) rn
8 from foo f
9 )
10 where rn = &n )
11 order by id
12 /
Enter value for n: 2
old 10: where rn = &n )
new 10: where rn = 2 )
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
SQL> r
1 select id
2 , group_id
3 from foo
4 where group_id <= ( select group_id
5 from (
6 select f.group_id
7 , row_number() over (order by f.id asc) rn
8 from foo f
9 )
10 where rn = &n )
11* order by id
Enter value for n: 4
old 10: where rn = &n )
new 10: where rn = 4 )
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
4 200
SQL> r
1 select id
2 , group_id
3 from foo
4 where group_id <= ( select group_id
5 from (
6 select f.group_id
7 , row_number() over (order by f.id asc) rn
8 from foo f
9 )
10 where rn = &n )
11* order by id
Enter value for n: 5
old 10: where rn = &n )
new 10: where rn = 5 )
ID GROUP_ID
---------- ----------
1 100
2 100
3 100
4 200
5 300
6 300
6 rows selected.
SQL>
如果我们假设group_id是连续的并且是递增的,那么@Shannon的答案将非常有效。 如果我们不做这个假设,并且我们有看起来像这样的数据,例如:
SQL> select * from foo order by id;
ID GROUP_ID
-- --------
1 100
2 100
3 100
4 200
6 100
7 400
9 500
10 500
11 500
12 600
这是一个棘手的问题。 例如,如果N = 3、4或5,则需要通过ID = 6获取行。对于N = 6,我们需要高达ID =7。对于N = 7,我们需要通过ID = 11。
我相信无论group_id的顺序如何,该查询都有效:
对于N = 7:
WITH q AS (SELECT ID, group_id
, row_number() OVER (ORDER BY ID) rn
, MAX(id) OVER (PARTITION BY group_id) rn2
FROM foo)
SELECT ID, group_id FROM q
WHERE ID <= (SELECT max(rn2) FROM q WHERE rn <= :N)
ORDER BY ID;
ID GROUP_ID
-- --------
1 100
2 100
3 100
4 200
6 100
7 400
9 500
10 500
11 500
9 rows selected
对于N = 6:
ID GROUP_ID
-- --------
1 100
2 100
3 100
4 200
6 100
7 400
对于N = 1:
ID GROUP_ID
-- --------
1 100
2 100
3 100
4 200
6 100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.