PgSQL高级

news/2024/7/9 21:23:52 标签: postgresql, 数据库

PgSQL高级

SQL执行顺序
(9) SELECT (10) DISTINCT col1,
[OVER()] (6) AGG_FUNC(col2)
(1) FROM table1
(3) JOIN table2
(2) ON table1.col = table2.col
(4) WHERE constraint_expression
(5) GROUP BY col
(7) WITH CUBE|ROLLUP
(8) HAVING constraint_expression
(11) ORDER BY col ASC |DESC
(12) LIMIT count OFFSET count
复制表结构(不复制表注释,其他都复制)
CREATE TABLE emp_copy (LIKE emp including all);
分组聚合

grouping sets扩展 (单个)

select os,device, city ,count(*)
from requests
group by grouping sets((os, device), (city), ());
上述语句等效于如下语句:
select os, device, NULL, count(*)
from requests group by os, device
union all
select NULL, NULL, NULL, count(*)
from requests
union all
select null, null, city, count(*)
from requests group by city;

rollup(嵌套)

select os,device, city ,count(*) from requests 
group by grouping sets((city), ROLLUP(os, device));
上述语句等效于如下语句:
select os,device, city ,count(*) from requests 
group by grouping sets((city), (os), (os, device), ());

cube(组合)

select os,device, city, count(*)
from requests 
group by cube (os, device, city);
上述语句等效于如下语句:
select os,device, city, count(*)
from requests 
group by grouping sets ((os, device, city),(os, device),(os, city),(device,city),(os),(device),(city),());
递归
with recursive tmp as (
    select id,name from user where id=1
    union all
    select test.id,test.name from test join tmp on test.id = tmp.id
) select * from tmp;
注意点
#1.修改表名,视图的表名也会跟着修改,重新备份表数据要特别注意
#2. col !='yyds' and col is null 需要判断null值(高斯数据库)
开窗
first_value() --第1个值
last_value() --最后1个值
lead(col,n,default) --往下n行
lag(col,n,default) --往上n行
row_number() --行序号
dense_rank() --并列,不占位
rank()  --并列,占位

#<窗口函数> over (partition by <用于分组的列名> order by <用于排序的列名> rows/range窗口子句)
rows/range between unbounded preceding and current row
preceding:往前
following:往后
current row:当前行
unbounded:无界限(起点或终点)
unbounded preceding:表示从前面的起点
unbounded following:表示到后面的终点
特殊函数
regexp_split_to_table(col,';')
decode(gender,1,'男',2,'女','未知')
nvl(col1,col2) 

http://www.niftyadmin.cn/n/5258577.html

相关文章

【网络安全】HTTP Slowloris攻击原理解析

文章目录 Slowloris攻击的概念Slowloris攻击原理Slowloris攻击的步骤其他的DDoS攻击类型UDP FloodICMP (Ping) FloodSYN FloodPing of DeathNTP AmplificationHTTP FloodZero-day DDoS 攻击 推荐阅读 Slowloris攻击的概念 Slowloris是在2009年由著名Web安全专家RSnake提出的一…

图像融合——现有比较火的网络

在深度学习中&#xff0c;用于图像融合的详细网络&#xff08;深度神经网络&#xff09;通常是为了学习如何结合来自多个源的信息以生成一个单一、增强的输出图像。这些网络可以基于不同的架构设计&#xff0c;下面是一些常用于图像融合任务的深度学习网络类型&#xff1a; 卷积…

投标中的评标打分事项分析

一、评标办法 本次招标方式为公开招标&#xff0c;采用竞争性磋商&#xff0c;投标单位需进行二次报价&#xff0c;根据报价得分和评委评分&#xff0c;选取总分最高的四家&#xff0c;投标报价作为评标的依据之一&#xff0c;招标人不保证最低价中标&#xff0c;报价部分占比6…

三、CM4树莓派文件传输及终端常用命令

一、文件传输 方法1&#xff1a;使用U盘 方法2&#xff1a;VNC文件传输 电脑文件传输到树莓派 树莓派文件传输到电脑 二、终端常用命令 sudo&#xff1a;以超级用户的身份来执行命令 sudo su #切换为超级用户身份 su pi #切换回普通用户pi 目录切换命令pwd#显示当前所在目…

小程序域名SSL证书能用免费的吗?

众所周知&#xff0c;目前小程序要求域名强制使用https协议&#xff0c;否则无法上线。但是对于大多数开发者来说&#xff0c;为每一个小程序都使用上付费的SSL证书&#xff0c;也是一笔不小的支出。那么小程序能使用免费的SSL证书吗&#xff1f; 答案是肯定的。目前市面上可选…

02 快速上手(Quick Start)

1、新建两个服务 starter&#xff1a;用于服务的触发&#xff08;类比生产者&#xff0c;业务触发入口&#xff09; starter/main.goworker&#xff1a;用户实际业务的执行&#xff08;类比消费者&#xff0c;实际执行&#xff09; worker/main.go 点击此处展开... 2、业务效…

Temporal 常见 FQ 速查

1、启动 worker 失败 INFO No Lofigured for temporal client. Created default one. Unable to create client failed reaching server: upstream connect error or disconnect/reset before headers. reset reason: connection failure 解决&#xff1a;没找到链接资源&…

第八章 SpringCloud Alibaba 实现微服务集成Sentinel

什么是Sentinel Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于服务容错的综合性解决方案。它以流量 为切入点, 从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。 Sentinel 具有以下特征: 丰富的应用场景&#xff1a;Sentinel 承接了阿里巴巴近 …