Sql Postgres-按组分页
假设我有一些数据:Sql Postgres-按组分页,sql,postgresql,pagination,Sql,Postgresql,Pagination,假设我有一些数据: id | stage | name ----+----------------------+------ 1 | pyramid | act 1 2 | pyramid | act 2 3 | pyramid | act 3 4 | other stage | act 4 5 | other stage | ac
id | stage | name
----+----------------------+------
1 | pyramid | act 1
2 | pyramid | act 2
3 | pyramid | act 3
4 | other stage | act 4
5 | other stage | act 5
6 | other stage | act 6
7 | other stage | act 7
8 | shangri la | act 8
我想使用分页将这些数据加载到我的UI中,但我希望分页处理的是组而不是行计数,实现这一点的最佳方法是什么
因此,第1页如果按节\u id分组将产生:
1 | pyramid | act 1
2 | pyramid | act 2
3 | pyramid | act 3
如果您可以指定所需的条件,则只需在WHERE子句中使用筛选器:
select *
from data
where stage = 'pyramid';
否则,如果需要页码,则需要确定分组条件的排名。你可以做:
select *
from data
where stage = (
select stage
from (
select stage, row_number() over ()
from data
group by stage
order by stage
) s
where row_number = <page number>
)
您希望它是动态的还是分区id是连续的而没有间隙的?如果是,分区id=$page\u number,$page\u number作为参数出现的位置很抱歉,这只是我从另一篇随机文章复制的数据。恐怕没有顺序段id。我需要分页的数据也是大量Sql的结果。我想我需要的是能够为组分配序列号……我已经更新了示例数据,以便更好地反映我的情况@JakubKania