数据库
首页 > 数据库> > SQL基础总结(背诵版)

SQL基础总结(背诵版)

作者:互联网

SQL基础教程

类型

基本语法

mysql -u root -p password
-- 一定记得加;
show databases;
use shop; -- 某一数据库
show tables;
show tables from shop;
desc product;-- 表名,查看表的结构
select version(); 

数据库&表的增删改

表的创建

格式

CREATE DATABASE<数据库名称>;
CREATE TABLE <表名> 
(<列名1> <数据类型> <该列所需约束>,
 <列名2> <数据类型> <该列所需约束>,
 <列名2> <数据类型> <该列所需约束>,
 …………
 <该表的约束1> <该表的约束2>…………);

命名规则

数据类型的指定

约束的设置

NOT NULL
NULL
PRIMARY KEY(<列名>)

表的删除和更新

DROP TABLE <表名>;
ALTER TABLE <表名> ADD COLUMN <列的定义>;
ALTER TABLE <表名> DROP COLUMN <列名>;
#非标准sql,Mysql
RENAME TABLE Poduct to Product;

插入数据

START TRANSACTION
INSERT INTO Product VALUES ('0001', 'T恤衫', '衣服', 1000, 500, '2009-09-20');
……
COMMIT;

查询基础

SELECT语句基础

列的查询(字句、换行)

SELECT <列名>,……
FROM <表名>;
-- 包含了SELECT和FROM两个子句,子句是SQL语句的组成要素,是以 SELECT或者FROM等作为起始的短语
-- 查询结果中列的顺序和SELECT 子句中的顺序相同
SELECT *
 FROM <表名>;
-- 如果使用星号的话,就无法设定列的显示顺序了。这时就会按照 CREATE TABLE语句的定义对列进行排序
-- 原则上希望大家能够以子句为单位进行换行(子句过长时,为方便起见可以换行)
-- 插入空行(无任何字符的行)会造成执行错误

为列设置别名

SELECT product_id AS id,
 product_name AS name,
 purchase_price AS price
 FROM Product;
SELECT product_id AS "商品编号",
 product_name AS "商品名称",
 purchase_price AS "进货单价"
 FROM Product;

常数的查询

SELECT '商品' AS string, 38 AS number, '2009-02-24' AS date, product_id, product_name
 FROM Product;

DISTINCT

SELECT DISTINCT product_type
 FROM Product;
 -- 可以在多列之前使用,多列一致消重
SELECT DISTINCT product_type, regist_date
 FROM Product;

WHERE字句

SELECT product_name, product_type
 FROM Product
 WHERE product_type = '衣服';

注释

-- 单行注释
/*
多行注释
任何注释都可以插在SQL语句(字句中间)中
*/

算术&比较运算符

算术运算符

比较运算符

逻辑运算符

聚合与排序

聚合函数

-- COUNT:计算表中的记录数(行数)
-- SUM: 计算表中数值列中数据的合计值
-- AVG: 计算表中数值列中数据的平均值
-- MAX: 求出表中任意列中数据的最大值
-- MIN: 求出表中任意列中数据的最小值
-- COUNT(*)会得到包含NULL的数据行数
select count(*)
 from product;
-- COUNT(<列名>)会得到NULL之外的数据行数
select count(purchase_price)
 from product;
-- 聚合函数会先将NULL排除在外,再进行计算
select sum(sale_price)
 from product;
select avg(sale_price)
 from product;
-- SUM/AVG 函数只能对数值类型的列使用,而MAX/MIN 函数原则上可以适用于任何数据类型的列(日期,字符串)
select max(sale_price),min(purchase_price)
 from product;
-- 想要计算值的种类时,可以在COUNT函数的参数中使用DISTINCT
-- 计算去除重复数据后的数据行数
select count(distinct product_type)
 from product;
-- 结果为3
-- 写在括号外的话,就会先计算出数据行数,然后再删除重复数据
select distinct count(product_type)
 from product;
-- 结果为8
-- SUM聚合函数使用DISTINCT
select sum(sale_price), sum(distinct sale_price)
 from product;

Group By子句

语法结构

select <列名1>,<列名2>,<列名3>, ……
 from <表名>
group by <列名1>,<列名2>,<列名3>,……;
-- exqmple
select product_type, count(*)
  from product
 group by product_type

包含NULL

WHERE&GROUP BY

select <列名1>,<列名2>,<列名3>, ……
  from <表名>
 where
 group by <列名1>,<列名2>,<列名3> ……;
-- example
select purchase_price, count(*)
  from product
 where product_type = '衣服'
 group by purchase_price;

聚合函数&GROUP BY子句常见错误

-- 错误示例
select product_type as pt, count(*)
  from product
 group by pt; 
-- 错误示例
select product_type, count(*)
  from product
 where count(*) = 2
 group by product_type;

HAVING子句

HAVING子句

select <列名1>,<列名2>,<列名3> ……
  from <表名>
 group by <列名1>,<列名2>,<列名3>, ……
 having <分组结果对应的条件>
-- 示例
select product_type, count(*)
  from product
 group by product_type
having count(*)=2;

HAVING子句构成要素

WHERE&HAVING?

-- HAVING=子句
select product_type, count(*)
  from product
 group by product_type
having product_type = '衣服'
-- WHERE子句
select product_type, count(*)
  from product
 where product_type = '依附'
 group by product_type; 

ORDER BY子句

ORDER BY子句

select <列名1>,<列名2>,<列名3>, ……
  from <表名>
 order by <排序基准列1>,<排序基准列2>,……;
-- 示例
select *
  from product
 order by sale_price;

升序降序

select *
  from prduct
 order by sale_price desc;

指定多个排序键

select *
  from product
 order by sale_price, product_id

NULL的顺序

在排序建中使用显示用的别名

select product_id as id, sale_price as sp, purchae_price
  from product
 order by sp, id;

ORDER BY子句中可以使用的列

-- SELECT子句中未使用的列
SELECT product_name, sale_price, purchase_price
 FROM Product
ORDER BY product_id;
-- 聚合函数
select product_type, count(*)
  from product 
 group by product_type
order by count(*);

不要使用列编号

-- 通过列名指定
SELECT product_id, product_name, sale_price, purchase_price
 FROM Product
ORDER BY sale_price DESC, product_id;
-- 通过列编号指定
SELECT product_id, product_name, sale_price, purchase_price
 FROM Product
ORDER BY 3 DESC, 1;

数据更新

INSERT语句

-- 创建表
CREATE TABLE ProductIns
(product_id CHAR(4) NOT NULL,
 product_name VARCHAR(100) NOT NULL,
 product_type VARCHAR(32) NOT NULL,
 sale_price INTEGER DEFAULT 0,
 purchase_price INTEGER,
 regist_date DATE,
 PRIMARY KEY (product_id));

语法结构

insert into <表名>(列1, 列2, 列3 ……) values (值1, 值2, 值3, ……);
-- 示例
insert into productins
 (product_id, product_name, product_type, sale_price, purchase_price, regist_date)
 values('0001','T恤衫, ’衣服',1000,500,'2009-09-20');

多行INSERT

-- 多行INSERT(Oracle以外)
INSERT INTO ProductIns VALUES
 ('0002', '打孔器', '办公用品', 500, 320, '2009-09-11'),
 ('0003', '运动T恤', '衣服', 4000, 2800, NULL),
 ('0004', '菜刀', '厨房用具', 3000, 2800, '2009-09-20');

列清单的省略

insert into productins values('0005','高压锅', '厨房用具', 6800, 5000, '2009-01-15')

插入NULL

插入默认值

显式插入
insert into productins
 values('007', '擦菜板', '厨房用具', default, 790, '2009-04-28');
隐式插入

从其他表中复制数据

-- 表结构与product相同
-- 用来插入数据的商品复制表
CREATE TABLE ProductCopy
(product_id CHAR(4) NOT NULL,
 product_name VARCHAR(100) NOT NULL,
 product_type VARCHAR(32) NOT NULL,
 sale_price INTEGER ,
 purchase_price INTEGER ,
 regist_date DATE ,
 PRIMARY KEY (product_id));
-- 将商品表中的数据复制到商品复制表中
INSERT INTO ProductCopy (product_id, product_name, product_type, sale_price, purchase_price, regist_date)
SELECT product_id, product_name, product_type, sale_price,purchase_price, regist_date
 FROM Product;

牛客网刷题所得

insert IGNORE into actor 
values(3,'ED','CHASE','2006-02-15 12:34:33');

DELETE语句

基本语法

-- 删除数据行
delete from <表名>;
-- 示例
delete from product
-- DELETE语句的删除对象并不是表或者列,而是记录(行)

搜索型DELETE

delete from <表名>
 where <条件>;
-- 可以通过WHERE子句指定对象条件来删除部分数据
-- 正是删除前确认要删除的结果

TRUNCATE

truncate <表名>;
执行速度比delete from <表名>;快

UPDATE语句

基本语法

update <表名>
   set <列名> = <表达式>;
-- 将更新对象的列和更新后的值都记述在 SET子句中
-- 示例
update product
   set regist_date = '2009-10-10';

搜索型UPDATE

update <表名>
   set <列名> = <表达式>
 where <条件>;
-- 示例
update product
   set sale_price = sale_price*10
 where product_type = '厨房用具';

NULL 清空

update product
   set regist_date = NULL
 where product_id = '008';

多列更新

-- 使用逗号对列进行分隔排列
UPDATE Product
   SET sale_price=sale_price*10,purchase_price=purchase_price/2
 WHERE product_type = '厨房用具';

事务

事务

语法结构

事务开始语句;
	DML语句1;
	DML语句2;
	DML语句3;
事务结束语句;
事务开始语句
start transaction;
	-- 将T恤衫的销售单价降低1000日元
	update product
	   set sale_price = sale_price - 1000
	 where product_name = 'T恤衫';
	 
	--将运动T恤的销售单价上浮1000日元
	update product
	   set sale_price = sale_price + 1000
	 where product_name = '运动T恤';
commit;
事务结束语句
COMMIT提交处理
ROLLBACK取消处理
start transaction
	-- 将T恤衫的销售单价降低1000日元
	update product
	   set sale_price = sale_price - 1000
	 where product_name = 'T恤衫';
	 
	--将运动T恤的销售单价上浮1000日元
	update product
	   set sale_price = sale_price + 1000
	 where product_name = '运动T恤';
rollback;

ACID特性

原子性Atomicity
一致性Consistency
隔离性Isolation
持久性Durability

复杂查询

视图

创建视图

create view 视图名称(<视图列名1>,<视图列名2>,<视图列名3>……)
as
<select 语句>
-- 示例
create view productview (product_type, cnt_product)
as
select product_type, count(*)
  from product
 group by product_type;
 -- 展示视图
 show tables;

使用视图

select product_type, cnt_product
  from productsum;

多重视图

cerate view productsumjim(product_type, cnt_product)
as
select product_type, cnt_product
  from productsum
 where product_type = '办公用品';

视图的限制

限制1:ORDER BY语句
限制2:对视图进行更新
CREATE VIEW ProductJim (product_id, product_name, product_type, sale_price, purchase_price, regist_date)
AS 
SELECT *
 FROM Product
 WHERE product_type = '办公用品';
INSERT INTO ProductJim VALUES ('0009', '印章', '办公用品', 95, 10, '2009-11-30');

删除视图

drop view 视图名称(<视图列名1>,<视图列名2>……);
-- 示例
drop view productview;

子查询

子查询

-- 示例
select product_type, cnt_product
  from (select product_type, count(*) as cnt_product
          from product
         group by product_type) as productsum

子查询名称

标量子查询

标量子查询
WHERE子句中使用标量子查询
select product_id, product_name, sale_price
  from product
 where sale_price > (select avg(sale_price)
                     from product);
书写位置
select product_id, product_name, sale_price, (select avg(sale_price) from product) as avg_price
  from product;
select product_type, avg(sale_price)
  from product
 group by product_type
having avg(sale_price) > (select avg(sale_price) from product);
注意事项

关联子查询

关联子查询

select product_type, product_name, sale_price
  from product as p1
 where sale_price > (select avg(sale_price)
                       from product as p2
                      where p1.product_type = p2.product_type);

集合切分

结合条件

-- 错误示例
SELECT product_type, product_name, sale_price
 FROM Product AS P1
 WHERE P1.product_type = P2.product_type
 AND sale_price > (SELECT AVG(sale_price)
 FROM Product AS P2
 GROUP BY product_type);

函数&谓词&CASE表达式

函数

算术函数

-- 绝对值函数:ABS(数值)
-- ABS 函数的参数为 NULL 时,结果也是 NULL
-- 并非只有 ABS 函数如此,其实绝大多数函数对于NULL都返回NULL
select m, abs(m) as abs_col
 from samplemath;
 
-- 求余:MOD(被除数,除数)
select n, p, mod(n,p) as mol_col
  from samplemath;

-- 四舍五入:round(对象数值, 保留小数的位数)
select m, n, round(m,n) as round_col
  from samplemath;

字符串函数

-- 拼接||
-- 进行字符串拼接时,如果其中包含 NULL,那么得到的结果也是NULL
-- 三个以上的字符串也可以进行拼接
select str1, str2, str3, concat(str1, str2, str3) as str_concat
from sampleStr;

-- 字符串长度length(str),以字节为单位
select str1, length(str1) as len_str
  from sampleStr;
-- 与半角英文字母占用 1 字节不同,汉字这样的全角字符会占用2个以上的字节(称为多字节字符)
-- 使用 MySQL 中的LENGTH 这样以字节为单位的函数进行计算时,“LENGTH( 山田 )”的返回结果是4
-- 同样是 LENGTH 函数,不同DBMS的执行结果也不尽相同

-- 小写转换lower(str)
-- 只针对英文字母使用
select str1, lower(str1) as low_str
  from sampleStr
 where str1 in ('ABC','abc','aBC','山田');

-- 大写转换upper(str)
select str1, upper(str1) as upp_str
  from sampleStr
 where str1 in ('ABC','abc','aBC','山田');

-- 字符串转换replace(对象字符串,替换前字符串,替换后字符串)
select str1, str2, str3, replace(str1, str2, str3) as rep_str
  from sampleStr;
-- 对str1做替换
-- str1&str2&str3中任一为空,结果为空
-- 若所有字段为非空
-- str1中若存在str2的部分,则str2的部分被全部替换为str3

-- 字符串截取substring(对象字符串 from 截取的起始位置 for 截取的字段数)
select str1, substring(str1 from 3 for 2) as sub_str
  from sampleStr;

日期函数

-- 当前日期current_date
select current_date;
--'2021-07-01'

-- 当前时间current_time
select current_time;
-- '22:06:25'

--当前时间和日期current_timestamp
select current_timestamp;
-- '2021-07-01 22:06:25'

-- 截取日期元素extract
-- 可以截取出日期数据的一部分,如年、月、日、时、分、秒,返回值为数值类型
-- extract(日期元素 from 日期)
select current_timestamp,
	extract(year from current_timestamp) as year,
	extract(month from current_timestamp) as month,
	extract(day from current_timestamp) as day,
	extract(hour from current_timestamp) as hour,
    extract(minute from current_timestamp)as minute,
    extract(second from current_timestamp)as second;

转换函数

-- 类型转换cast
-- cast(转换前的值,想要转换的数据类型)
select cast('0001' as integer) as int_col;
select cast('2021-07-01' as date) as date_col;

-- 将NULL转换为其他值coalesce
-- coalesce(数据1,数据2,数据3……)
-- 该函数会返回可变参数中左侧开始第1个不是NULL的值
-- 参数个数是可变的,因此可以根据需要无限增加
select coalesce(NULL,1) as col1, coalesce(NULL, 'test', NULL) as col2, coalesce(NULL, NULL, '2021-07-02') as col3;
select coalesce(str2,NULL)
  from sampleStr;

谓词

LIKE谓词

部分一致
通配符
实现
-- 前方一致查询
select * 
  from sampleLike
 where strcol like'ddd%';
 
-- 中间一致查询
select * 
  from sampleLike
 where strcol like '%ddd%';
 
-- 后方一致查询
select * 
  from sampleLike
 where strcol like '%ddd';
 
-- -单个字符
select * 
  from sampleLike
 where strcol like 'ddd--';

BETWEEN

-- 三个参数
select product_name, product_price
  from product
 where product_price between 100 and 1000;
-- 结果包含临界值

-- 如果不想让结果中包含临界值,就必须使用 < 和 >
select product_name, product_price
  from product
 where product_price > 100 and product_price < 1000;

IS NULL& IS NOT NULL

select product_name, purchase_price
  from product
 where purchase_price is null;
 
select product_name, purchase_price
  from product
 where purchase_price is null;

IN

-- in(值1,值2,……)
select product_name, purchase_price
  from product
 where purchase_price in (320, 500, 1000);
 
-- not in 
select product_name, purchase_price
  from product
 where purchase_price in (320, 500, 1000);

-- 在使用IN和NOT IN时是无法选取出NULL数据的
使用子查询作为IN谓词的参数
-- primary key(col1, col2) 把属性集作为主键
-- in
select product_name, sale_price
  from product
 where product_id in (select product_d 
                        from shopproduct
                       where shop_id = '000C');
-- 易维护程序&免维护程序

-- not in
select product_name, sale_price
  from product
 where product_id not in (select product_d 
                        from shopproduct
                       where shop_id = '000A');

EXISTS

-- exist
-- 判断是否存在满足某种条件的记录,如果存在这样的记录就返回真(TRUE),如果不存在就返回假(FALSE)
select product_name, sale_price
  from product as p
 where exists(select *
                from shopproduct as sp
               where sp.shop_id  = '000C'
                and sp.product_id = p.product_id);
-- EXIST 通常都会使用关联子查询作为参数
-- 主语是记录-> select *

-- not exist
select product_name, sale_price
  from product as P
 where not exist(select * 
                   from shopproduct as sp
                  where sp.shop_id = '000A'
                    and sp.product_id = p.product_id);
-- NOT EXIST与EXIST相反,当不存在满足子查询中指定条件的记录时返回真(TRUE)

CASE表达式

搜索case表达式语法

case when <求值表达式> then<表达式>
	 when <求值表达式> then<表达式>
	 ...
	 else<表达式>
end
-- 求值表达式:返回值为真值(TRUE/FALSE/UNKNOWN)的表达式;也可以将其看作使用 =、!= 或者 LIKE、BETWEEN 等谓词编写出来的表达式
-- 表达式:表达式最终会返回一个值

搜索case表达式使用方法

select product_name,
	case when product_type = '衣服'
    	 then concat('A',product_type)
    	 when product_type = '办公用品'
    	 then concat('B',product_type)
    	 when product_type = '厨房用具'
    	 then concat('C',product_type)
    	 else NULL
    end as product_type
    from product;
-- ELSE子句也可以省略不写,这时会被默认为ELSE NULL
-- 为了防止有人漏读,还是希望大家能够显示地写出ELSE子句
-- CASE表达式中的END不能省略
-- 对按照商品种类计算出的销售单价合计值进行行列转换
select sum(case when product_type = '衣服' then sale_price else NULL end) as sum_price_cloth,
	   sum(case when product_type = '厨房用具' then sale_price else NULL end) as sum_price_chicken,
	   sum(case when product_type = '办公用品' then sale_price else NULL end) as sum_price_office
  from product;
  
-- 练习题
select sum(case when sale_price <= 1000 then 1 else NULL end) as low_price,
	   sum(case when 1000 < sale_price and sale_price <= 3000 then 1 else NULL end) as mid_price,
	   sum(case when 3000 < sale_price then 1 else NULL end) as high_price
  from product;

简单case表达式

-- 语法
CASE <表达式>
 WHEN <表达式> THEN <表达式>
 WHEN <表达式> THEN <表达式>
 WHEN <表达式> THEN <表达式>
 . . .
 ELSE <表达式>
END

-- 简单CASE表达式示例
SELECT product_name,
 CASE product_type
 WHEN '衣服' THEN 'A :' | | product_type
 WHEN '办公用品' THEN 'B :' | | product_type
 WHEN '厨房用具' THEN 'C :' | | product_type
 ELSE NULL
 END AS abc_product_type
 FROM Product;

特殊CASE表达式

-- MySQL中使用IF代替CASE表达式
-- 只能在特定的 DBMS 中使用,并且能够使用的条件也没有CASE表达式那么丰富,因此并没有什么优势
-- 希望大家尽量不要使用这些特定的SQL语句

集合运算

集合运算

表的加减法

加法-UNION
-- 示例,并集,可以通过文氏图展示
select product_id, product_name
  from product
 union
select product_id, product_name
  from product2
集合运算的注意事项
-- 列数不一致时会发生错误
SELECT product_id, product_name
 FROM Product
UNION
SELECT product_id, product_name, sale_price
 FROM Product2;
-- 数据类型不一致时会发生错误
SELECT product_id, sale_price
 FROM Product
UNION
SELECT product_id, regist_date
 FROM Product2;
select product_id, product_name
  from proudct
 where product_type = '厨房用具'
 union
select product_id, product_name
  from product2
 where product_type = '厨房用具'
 order by product_id;
包含重复行的集合运算-ALL选项
-- 保留重复行
select product_id, product_name
  from product
 union all
select product_id, product_name
  from product2
交集INTERSECT&差EXCEPT
-- 交集运算
SELECT product_id, product_name
 FROM Product
INTERSECT
SELECT product_id, product_name
 FROM Product2
ORDER BY product_id;

-- 差集运算
SELECT product_id, product_name
 FROM Product
EXCEPT
SELECT product_id, product_name
 FROM Product2
ORDER BY product_id;

联结(以列为单位对表进行联结)

内联接INNER JOIN
-- 示例
select sp.shop_id, sp.shop_name, sp.product_id, p.product_name, p.sqle_price
  from shopproduct as sp inner join product as p 
    on sp.product_id = p.product_id;

-- FORM子句
-- 进行联结时需要在FROM子句中使用多张表,使用关键字INNER JOIN就可以将两张表联结在一起了
-- SP和P分别是这两张表的别名,别名并不是必需的,为了增强可读性,建议使用别名

-- ON子句
-- 进行内联结时必须使用ON子句,并且要书写在FROM和WHERE之间
-- 在ON之后指定两张表联结所使用的列(联结键),ON 是专门用来指定联结条件的,它能起到与 WHERE 相同的作用
-- 联结条件也可以使用 = 来记述,在语法上,还可以使用<=和BETWEEN等谓词

-- SELECT子句
-- 使用联结时SELECT子句中的列需要按照“<表的别名>.<列名>”的格式进行书写
select sp.shop_id, sp.shop_name, sp.product_id, p.product_name, p.sale_price
  from shopproduct as sp inner join product as p 
    on sp.product_id = p.product_id
 where sp.shop_id = '000A';
外联结OUTER JOIN
--示例 right outer join
SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price
 FROM ShopProduct AS SP RIGHT OUTER JOIN Product AS P
 ON SP.product_id = P.product_id;
-- 示例 left outer join
SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price
 FROM Product AS P LEFT OUTER JOIN ShopProduct AS SP
 ON SP.product_id = P.product_id;
select case when SP.shop_id is not null
			then SP.shop_id
			else 'unknown'
			end,
		case when SP.shop_id is not null
			 then SP.shop_name
			 else 'unknown'
			 end
		, SP.product_id, P.product_name
  from shopproduct as sp right outer join product as p
    on sp.product_id = p.product_id;
    
select SP.shop_id, SP.shop_name, SP.product_id, P.product_name
  from shopproduct as sp left outer join product as p
    on sp.product_id = p.product_id;
3张以上表的联接
-- 示例
SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price, IP.inventory_quantity
 FROM ShopProduct AS SP INNER JOIN Product AS P 
 ON SP.product_id = P.product_id
     INNER JOIN InventoryProduct AS IP 
     ON SP.product_id = IP.product_id
 WHERE IP.inventory_id = 'P001';
交叉联结CROSS JOIN
-- 示例
SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name
 FROM ShopProduct AS SP CROSS JOIN Product AS P;
过时语法
-- 使用过时语法的内联结
SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, 
P.sale_price
 FROM ShopProduct SP, Product P
 WHERE SP.product_id = P.product_id
 AND SP.shop_id = '000A';

SQL高级处理

窗口函数

窗口函数语法
<窗口函数> OVER ([PARTITION BY <列清单>] 
 ORDER BY <排序用列清单>)
-- []中的内容可以省略 
-- 其中重要的关键字是PARTITION BY和ORDER BY
RANK函数-基本使用方法
-- 增加一列ranking,表示顺序
select product_name, product_type, sale_price, 
		rank() over(partition by product_type
                   order by sale_price) as ranking
  from product;
PARTITION BY 并不是必需的
select product_name, product_type, sale_price, 
		rank() over( order by sale_price) as ranking
  from product;
专用窗口函数
select product_name, product_type, sale_price,
	rank() over(order by sale_price) as ranking,
	dense_rank() over(order by sale_price) as dense_ranking,
	row_number() over(order by sale_price) as row_numbering
  from product;
窗口函数的适用范围
作为窗口函数使用的聚合函数
select product_id, product_name, sale_price,
	sum(sale_price) over (order by product_id) as current_sum
from product;
select product_name, product_type, sale_price,
	avg(sale_price) over(order by product_id) as current_avg
  from product;
计算移动平均
-- 需要在ORDER BY子句之后使用指定范围的关键字
-- 使用了ROWS行和PRECEDING之前两个关键字,将框架指定为“截止到之前~行”
-- 将框架指定为“截止到之前2行”,也就是将作为汇总对象的记录限定为如下的“最靠近的3行”
SELECT product_id, product_name, sale_price,
 AVG (sale_price) OVER (ORDER BY product_id
 						ROWS 2 PRECEDING) AS moving_avg
 FROM Product;
select product_name, product_type, sale_price,
	avg(sale_price) over(order by product_id
                        rows between 1 preceding and 1 following) as moving_avg
  from product;
两个GROUP BY

GROUPING函数

select '合计' as product_type, sum(sale_price)
  from product
union all 
select product_type, sum(sale_price) 
  from product
 group by product_type;
ROLLUP-同时得出合计和小计
-- 示例1
SELECT product_type, SUM(sale_price) AS sum_price
 FROM Product
GROUP BY product_type WITH ROLLUP;
-- 一次计算出了如下两种组合的汇总结果
-- GROUP BY ()
-- GROUP BY (product_type)

-- 示例2
select product_type, regist_data, sum(sale_price) as sum_price
  from product
 group by product_type, regist_data with rollup;
-- 如下三种组合的汇总结果
-- GROUP BY ()
-- GROUP BY (product_type) 
-- GROUP BY (product_type, regist_date)
GROUPING函数-让NULL更容易区分
SELECT GROUPING(product_type) AS product_type, 
       GROUPING(regist_data) AS regist_date,
       SUM(sale_price) AS sum_price
 FROM Product 
GROUP BY product_type, regist_data with rollup;
SELECT CASE WHEN GROUPING(product_type) = 1 
 THEN '商品种类 合计' 
 ELSE product_type END AS product_type,
 CASE WHEN GROUPING(regist_data) = 1 
 THEN '登记日期 合计' 
 ELSE CAST(regist_data AS char(16)) END AS regist_data,
 SUM(sale_price) AS sum_price
 FROM Product
 GROUP BY product_type, regist_data with rollup;
CUBE-用数据来搭积木
SELECT CASE WHEN GROUPING(product_type) = 1 
 THEN '商品种类 合计'
 ELSE product_type END AS product_type,
 CASE WHEN GROUPING(regist_data) = 1 
 THEN '登记日期 合计'
 ELSE CAST(regist_data AS CHAR(16)) END AS regist_date,
 SUM(sale_price) AS sum_price
 FROM Product
GROUP BY CUBE(product_type, regist_date);
GROUPING SETS-取得期望的积木
SELECT CASE WHEN GROUPING(product_type) = 1 
 THEN '商品种类 合计'
 ELSE product_type END AS product_type,
 CASE WHEN GROUPING(regist_date) = 1 
 THEN '登记日期 合计'
 ELSE CAST(regist_date AS VARCHAR(16)) END AS regist_date,
 SUM(sale_price) AS sum_price
 FROM Product
 GROUP BY GROUPING SETS (product_type, regist_date);

标签:总结,product,--,price,背诵,SQL,type,id,select
来源: https://blog.csdn.net/weixin_42880377/article/details/118469056