SQL 笔记

create table t1(id int,name varchar2(10));

insert into t1 values(1,'aaa');

insert into t1 values(2,'bbb');

insert into t1 values(3,'ccc');

insert into t1 values(4,'ddd');

insert into t1 values(5,'eee');

commit;

create table t1_a as select * from t1 where 1=2;

create table t1_b as select * from t1 where 1=2;
无条件多表插入:

insert all into t1_a into t1_b select * from t1;
有条件多表插入:

insert all when id<3 then into t1_a else into t1_b select * from t1;

优先插入:

insert first when id<3 then into t1_a when id>1 then into t1_b select * from t1;

旋转插入:行列转换

create table sales(id number,name varchar2(10),q1 number,q2 number,q3 number,q4 number);

insert into sales values(1,'apple',1000,2000,3000,4000);

insert into sales values(2,'orange',2000,2000,4000,4000);

insert into sales values(3,'grape',1500,2000,3600,4000);

insert into sales values(4,'banana',1900,2000,3900,4000);

commit;

create table sales_info(id number,name varchar2(10),quarter varchar2(5),smount number);

insert all

into sales_info values(id,name,'q1',q1)

into sales_info values(id,name,'q2',q2)

into sales_info values(id,name,'q3',q3)

into sales_info values(id,name,'q4',q4)

select id,name,q1,q2,q3,q4 from sales;

列转行:

select * from ((select id,name,smount,quarter from sales_info) pivot(sum(smount) for quarter in ('q1' as q1,'q2' as q2,'q3' as q3,'q4' as q4)))

SELECT 非聚合列
FROM 源表或子查询
PIVOT (
聚合函数(待聚合列) -- 对数据进行聚合(如SUM、COUNT、AVG等)
FOR 行转列的基准列 -- 指定哪一列的取值将转换为新列
IN (值1 AS 列名1, 值2 AS 列名2, ...) -- 明确要转换为列的具体值及对应列名
);

with 子句:

WITH aaaa AS (SELECT * FROM "SYSIBM".SYSDUMMY1 s), bbbb AS (SELECT * FROM aaaa) SELECT * FROM bbbb;

分页查询公式:
分页查询起始行=(页码 - 1)*每页长度 + 1
分页查询中止行= 页码*每页长度

跳过前5行,取后面5行:(12C才有的功能)

select * from jk order by sn offset 5 rows fetch next 5 rows only;

前10%的记录:

select * from jk order by sal desc fetch first 10 percent rows only;

如果有并行的值,也要显示出来(即使超出了实际规定的百分比)

select * from jk order by sal desc fetch first 10 percent rows with ties;

如果不喜欢自己换算时间,则用timestamp with local time zone 会将用户输入的时间转换为数据库服务器所在时区的时间
如果喜欢保存原本的时区信息,则用timestamp with time zone

create table meeting(name varchar2(10),

time1 timestame,

time2 timestamp with time zone,

time3 timestame with local time zone);

相关推荐
浅念-7 分钟前
Redis基础详解:单线程模型、String与Hash数据结构
服务器·数据库·redis·sql·mysql·nosql数据库·nosql
天天被压力7 分钟前
【跨市场数据实战 #08】可转债折价机会怎么筛:3个接口抓比价、列表和实时盘口
java·人工智能·python
updayday85420 分钟前
离职域账号状态变更与Ping64操作记录核对
大数据·网络·数据库·安全·智能路由器
Nturmoils43 分钟前
只恢复一张表,别把整个库都还回去
数据库
张某布响丸辣1 小时前
附件下载的安全边界:路径白名单、NAS 哨兵文件与 Redis Lua 一次性令牌
java·redis·redis lua·nas哨兵
龙腾AI白云1 小时前
大语言模型:从语言理解到通用智能的跃迁
数据库·人工智能·机器学习·知识图谱
YatHinLay1 小时前
Spring Boot + Calcite 实现跨库查询
java
SelectDB2 小时前
快手基于 Apache Doris 千亿多模态检索的实践
大数据·数据库·数据分析
酷炫码神2 小时前
MosMos 创意效果与能力边界展示
java
SelectDB2 小时前
StarRocks 迁移至 Apache Doris 完整指南:三步完成结构、数据与业务平滑切换
大数据·数据库·数据分析