update
This commit is contained in:
@@ -24,3 +24,14 @@ ALTER DATABASE kt_shop REFRESH COLLATION VERSION;
|
||||
|
||||
-- 重置某表的某列的自增序列 [表名]_[列名]_seq
|
||||
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;
|
||||
-- 得到
|
||||
22
PostgreSQL/windows_fun.sql
Normal file
22
PostgreSQL/windows_fun.sql
Normal 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会不断+1,row_number()也会不断+1,id - 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;
|
||||
Reference in New Issue
Block a user