1
0
This commit is contained in:
2025-11-04 21:34:48 +08:00
parent afaa00c056
commit ed8630d100
2 changed files with 33 additions and 0 deletions

View File

@@ -24,3 +24,14 @@ ALTER DATABASE kt_shop REFRESH COLLATION VERSION;
-- 重置某表的某列的自增序列 [表名]_[列名]_seq -- 重置某表的某列的自增序列 [表名]_[列名]_seq
ALTER SEQUENCE person_id_seq RESTART WITH 4284858; ALTER SEQUENCE person_id_seq RESTART WITH 4284858;
-- ctid 会返回一个元组,元组中第一个数字是 page number, 第二个是 slot number。
select r.ctid, r.* from study.r;
-- 得到
-- "(0,1)",1,101
-- "(0,2)",2,102
-- "(0,3)",3,103
-- 删除一条
delete from study.r where id = 101;
-- 得到

View File

@@ -0,0 +1,22 @@
-- 窗口函数
-- row_number(): 对相等的值【不区分】相当于行号相等的值对应的排名不同序号从1到n连续
-- rank(): 相等的值排名相同但若有相等的值则序号从1到n不连续。符合一般人思维中的排名
-- dense_rank(): 对相等的值排名相同但序号从1到n连续
-- 先按num分区每个区内按id排序
select id, num, id - row_number() over (partition by num order by id) as rnk
from employee;
-- 找出所有至少连续出现三次的数字
-- 先按num分区每个区内按id排序
-- 一个分区内如果数字连续那么id会不断+1row_number()也会不断+1id - row_number()差值不变
-- 在按照id - row_number()聚合使用count(*)统计count(*)的结果就是连续出现的次数
select r.num, r.rnk, count(*)
from (select num, id - row_number() over (partition by num order by id) as rnk from employee) as r
group by r.num, r.rnk;
select distinct r.num as ConsecutiveNums
from (select num, id - row_number() over (partition by num order by id) as rnk from employee) as r
group by r.num, r.rnk
having count(*) >= 3
limit 1;