postgresql获取每组前几条记录
postgresql获取每组前几条记录:
示例
表结构如下:
Table "ytt.t1"Column | Type | Modifiers
--------+-----------------------+-----------
i_name | character varying(10) | not null
rank | integer | not null
模拟了20条数据来做演示:
t_girl=# select * from t1 order by i_name;i_name | rank
---------+------
Charlie | 12
Charlie | 12
Charlie | 13
Charlie | 10
Charlie | 11
Lily | 6
Lily | 7
Lily | 7
Lily | 6
Lily | 5
Lily | 7
Lily | 4
Lucy | 1
Lucy | 2
Lucy | 2
Ytt | 14
Ytt | 15
Ytt | 14
Ytt | 14
Ytt | 15
(20 rows)
按照人名分组,每组取3条记录:
select *from (select i_name, rank, row_number() over(partition by i_name) as row from t1) t
where row < =3
SELECT 语句用于从数据库中选取数据。
结果被存储在一个结果表中,称为结果集。
SQL SELECT 语法
SELECT column_name,column_nameFROM table_name;
与
SELECT * FROM table_name;
SQL WHERE 子句
WHERE 子句用于提取那些满足指定条件的记录。
SQL WHERE 语法
SELECT column_name,column_nameFROM table_name
WHERE column_name operator value;
推荐:PostgreSQL教程
以上是 postgresql获取每组前几条记录 的全部内容, 来源链接: utcz.com/z/539046.html