写在前面

关系模型的数据结构里,并没有顺序的概念,但SQL处理有序集合也有坚实的理论基础

生成连续编号

--生成连续编号
CREATE TABLE Digits
(digit INTEGER PRIMARY KEY); INSERT INTO Digits VALUES (0);
INSERT INTO Digits VALUES (1);
INSERT INTO Digits VALUES (2);
INSERT INTO Digits VALUES (3);
INSERT INTO Digits VALUES (4);
INSERT INTO Digits VALUES (5);
INSERT INTO Digits VALUES (6);
INSERT INTO Digits VALUES (7);
INSERT INTO Digits VALUES (8);
INSERT INTO Digits VALUES (9);
-- 创建00-99的序列
SELECT (D1.digit + D2.digit * 10) AS seq
FROM Digits AS D1 CROSS JOIN Digits AS D2
ORDER BY seq; -- 创建1到542的序列
SELECT (D1.digit + D2.digit * 10 + D3.digit * 100) AS seq
FROM Digits AS D1,Digits AS D2,Digits AS D3
WHERE (D1.digit + D2.digit * 10 + D3.digit * 100) BETWEEN 1 AND 542
ORDER BY seq;
CREATE VIEW Sequence (seq) AS
SELECT D1.digit + (D2.digit * 10 ) + (D3.digit * 100)
FROM Digits D1,Digits D2,Digits D3; SELECT seq FROM Sequence WHERE seq BETWEEN 1 AND 100 ORDER BY seq;

求全部的缺失编号

-- EXCEPT版
SELECT seq FROM Sequence WHERE seq BETWEEN 1 AND 12
EXCEPT (SELECT * FROM SeqTbl);
-- NOT IN版
SELECT seq FROM Sequence WHERE seq BETWEEN 1 AND 12 AND seq
NOT IN (SELECT * FROM SeqTbl);
-- 动态地指定连续编号范围的SQL语句
SELECT seq FROM Sequence WHERE seq BETWEEN (SELECT MIN(seq) FROM SeqTbl) AND (SELECT MAX(seq) FROM SeqTbl) EXCEPT (SELECT * FROM SeqTbl);

三个人能坐得下吗?

--三个人能坐得下吗?
CREATE TABLE Seats
( seat INTEGER NOT NULL PRIMARY KEY,
status CHAR(6) NOT NULL
CHECK (status IN ('未预订', '已预订')) ); INSERT INTO Seats VALUES (1, '已预订');
INSERT INTO Seats VALUES (2, '已预订');
INSERT INTO Seats VALUES (3, '未预订');
INSERT INTO Seats VALUES (4, '未预订');
INSERT INTO Seats VALUES (5, '未预订');
INSERT INTO Seats VALUES (6, '已预订');
INSERT INTO Seats VALUES (7, '未预订');
INSERT INTO Seats VALUES (8, '未预订');
INSERT INTO Seats VALUES (9, '未预订');
INSERT INTO Seats VALUES (10, '未预订');
INSERT INTO Seats VALUES (11, '未预订');
INSERT INTO Seats VALUES (12, '已预订');
INSERT INTO Seats VALUES (13, '已预订');
INSERT INTO Seats VALUES (14, '未预订');
INSERT INTO Seats VALUES (15, '未预订');
-- 找出需要的空位(1):不考虑座位的换排
SELECT S1.seat AS start_seat,'~',S2.seat AS end_seat
FROM Seats S1,Seats S2
WHERE S2.seat = S1.seat + 2 AND NOT EXISTS
(SELECT * FROM Seats S3 WHERE S3.seat BETWEEN S1.seat AND S2.seat AND S3.status <> '未预订');
--考虑座位的折返
CREATE TABLE Seats2
( seat INTEGER NOT NULL PRIMARY KEY,
row_id CHAR(1) NOT NULL,
status CHAR(6) NOT NULL
CHECK (status IN ('未预订', '已预订')) ); INSERT INTO Seats2 VALUES (1, 'A', '已预订');
INSERT INTO Seats2 VALUES (2, 'A', '已预订');
INSERT INTO Seats2 VALUES (3, 'A', '未预订');
INSERT INTO Seats2 VALUES (4, 'A', '未预订');
INSERT INTO Seats2 VALUES (5, 'A', '未预订');
INSERT INTO Seats2 VALUES (6, 'B', '已预订');
INSERT INTO Seats2 VALUES (7, 'B', '已预订');
INSERT INTO Seats2 VALUES (8, 'B', '未预订');
INSERT INTO Seats2 VALUES (9, 'B', '未预订');
INSERT INTO Seats2 VALUES (10,'B', '未预订');
INSERT INTO Seats2 VALUES (11,'C', '未预订');
INSERT INTO Seats2 VALUES (12,'C', '未预订');
INSERT INTO Seats2 VALUES (13,'C', '未预订');
INSERT INTO Seats2 VALUES (14,'C', '已预订');
INSERT INTO Seats2 VALUES (15,'C', '未预订');
-- 找出需要的空位(2):考虑座位的换排
SELECT S1.seat AS start_seat,'~',S2.seat AS end_seat
FROM Seats2 S1,Seats2 S2
WHERE S2.seat = S1.seat + 2 AND NOT EXISTS
(SELECT * FROM Seats2 S3 WHERE S3.seat BETWEEN S1.seat AND S2.seat AND (S3.status <> '未预订' OR S3.row_id <> S1.row_id));

最多能坐下多少人

--最多能坐下多少人?
CREATE TABLE Seats3
( seat INTEGER NOT NULL PRIMARY KEY,
status CHAR(6) NOT NULL
CHECK (status IN ('未预订', '已预订')) ); INSERT INTO Seats3 VALUES (1, '已预订');
INSERT INTO Seats3 VALUES (2, '未预订');
INSERT INTO Seats3 VALUES (3, '未预订');
INSERT INTO Seats3 VALUES (4, '未预订');
INSERT INTO Seats3 VALUES (5, '未预订');
INSERT INTO Seats3 VALUES (6, '已预订');
INSERT INTO Seats3 VALUES (7, '未预订');
INSERT INTO Seats3 VALUES (8, '已预订');
INSERT INTO Seats3 VALUES (9, '未预订');
INSERT INTO Seats3 VALUES (10, '未预订');
-- 先生成存储了所有序列的视图
CREATE VIEW Sequences(start_seat,end_seat,seat_cnt) AS
SELECT S1.seat AS start_seat,S2.seat AS end_seat,S2.seat-S1.seat + 1 AS seat_cnt
FROM Seats3 S1,Seats3 S2
WHERE S1.seat < S2.seat
AND NOT EXISTS (SELECT * FROM Seats3 S3 WHERE (S3.seat BETWEEN S1.seat AND S2.seat AND S3.status<> '未预订') OR (S3.seat = S2.seat + 1 AND S3.status = '未预订') OR (S3.seat = S1.seat - 1 AND S3.status = '未预订')); -- 取出最长的序列
SELECT start_seat,end_seat,seat_cnt FROM
Sequences WHERE seat_cnt = (SELECT MAX(seat_cnt) FROM Sequences);

单调递增和单调递减

--单调递增和单调递减
CREATE TABLE MyStock
(deal_date DATE PRIMARY KEY,
price INTEGER ); INSERT INTO MyStock VALUES ('2007-01-06', 1000);
INSERT INTO MyStock VALUES ('2007-01-08', 1050);
INSERT INTO MyStock VALUES ('2007-01-09', 1050);
INSERT INTO MyStock VALUES ('2007-01-12', 900);
INSERT INTO MyStock VALUES ('2007-01-13', 880);
INSERT INTO MyStock VALUES ('2007-01-14', 870);
INSERT INTO MyStock VALUES ('2007-01-16', 920);
INSERT INTO MyStock VALUES ('2007-01-17', 1000);
-- 生成起点到终点的组合
SELECT My1.deal_date,My2.deal_date
FROM MyStock AS My1,MyStock AS My2
WHERE My1.deal_date < My2.deal_date
AND NOT EXISTS
(SELECT * FROM MyStock AS My3,MyStock AS My4
WHERE My3.deal_date BETWEEN My1.deal_date AND My2.deal_date
AND My3.deal_date BETWEEN My1.deal_date AND My2.deal_date
AND My3.deal_date < My4.deal_date
AND My3.price >= My4.price);
--排除掉子集,只取最长的时间区间
SELECT MIN(start_date) AS start_date, /* 最大限度地向前延伸起点 */
end_date
FROM (SELECT S1.deal_date AS start_date,
MAX(S2.deal_date) AS end_date /* 最大限度地向后延伸终点 */
FROM MyStock S1, MyStock S2
WHERE S1.deal_date < S2.deal_date
AND NOT EXISTS
(SELECT *
FROM MyStock S3, MyStock S4
WHERE S3.deal_date BETWEEN S1.deal_date AND S2.deal_date
AND S4.deal_date BETWEEN S1.deal_date AND S2.deal_date
AND S3.deal_date < S4.deal_date
AND S3.price >= S4.price)
GROUP BY S1.deal_date) TMP
GROUP BY end_date
ORDER BY start_date;

小结

  • SQL处理数据的方法有两种
  • 第一种把数据看成忽略了顺序的集合
  • 第二种把数据看成有序的集合,此时的基本方法如下:
    • 首先自连接生成起点到终点的集合
    • 其次在子查询中描述内部的各个元素之间必须满足的关系
  • 要在SQL中表达全称量化时,需要将全称量化命题转化为存在量化命题的否定形式,并使用NOT EXISTS谓词。

练习题

/* 练习题1-9-1:求所有的缺失编号——NOT EXISTS和外连接
NOT EXISTS版 */
SELECT seq
FROM Sequence N
WHERE seq BETWEEN 1 AND 12
AND NOT EXISTS
(SELECT *
FROM SeqTbl S
WHERE N.seq = S.seq );
/* 练习题1-9-1:求所有的缺失编号——NOT EXISTS和外连接
NOT EXISTS版 */
SELECT N.seq
FROM Sequence N LEFT OUTER JOIN SeqTbl S
ON N.seq = S.seq
WHERE N.seq BETWEEN 1 AND 12
AND S.seq IS NULL;
/* 练习题1-9-2:求序列——面向集合的思想 */
SELECT S1.seat AS start_seat, '~' , S2.seat AS end_seat
FROM Seats S1, Seats S2, Seats S3
WHERE S2.seat = S1.seat + (:head_cnt -1)
AND S3.seat BETWEEN S1.seat AND S2.seat
GROUP BY S1.seat, S2.seat
HAVING COUNT(*) = SUM(CASE WHEN S3.status = '未预订' THEN 1 ELSE 0 END);
/* 坐位有换排时 */
SELECT S1.seat AS start_seat, ' ~ ' , S2.seat AS end_seat
FROM Seats2 S1, Seats2 S2, Seats2 S3
WHERE S2.seat = S1.seat + (:head_cnt -1)
AND S3.seat BETWEEN S1.seat AND S2.seat
GROUP BY S1.seat, S2.seat
HAVING COUNT(*) = SUM(CASE WHEN S3.status = '未预订'
AND S3.row_id = S1.row_id THEN 1 ELSE 0 END);
/* 练习题1-9-3:求所有的序列——面向集合的思想 */
SELECT S1.seat AS start_seat,
S2.seat AS end_seat,
S2.seat - S1.seat + 1 AS seat_cnt
FROM Seats3 S1, Seats3 S2, Seats3 S3
WHERE S1.seat <= S2.seat /* 第一步:生成起点和终点的组合 */
AND S3.seat BETWEEN S1.seat - 1 AND S2.seat + 1
GROUP BY S1.seat, S2.seat
HAVING COUNT(*) = SUM(CASE WHEN S3.seat BETWEEN S1.seat AND S2.seat
AND S3.status = '未预订' THEN 1 /* 条件1 */
WHEN S3.seat = S2.seat + 1 AND S3.status = '已预订' THEN 1 /* 条件2 */
WHEN S3.seat = S1.seat - 1 AND S3.status = '已预订' THEN 1 /* 条件3 */
ELSE 0 END);

SQL进阶系列之9用SQL处理数列的更多相关文章

  1. SQL进阶系列之11让SQL飞起来

    写在前面 SQL的性能优化是数据库使用者必须面对的重要问题,本节侧重SQL写法上的优化,SQL的性能同时还受到具体数据库的功能特点影响,这些不在本节讨论范围之内 使用高效的查询 参数是子查询时,使用E ...

  2. SQL进阶系列之7用SQL进行集合运算

    写在前面 集合论是SQL语言的根基,因为这种特性,SQL也被称为面向集合语言 导入篇:集合运算的几个注意事项 注意事项1:SQL能操作具有重复行的集合(multiset.bag),可以通过可选项ALL ...

  3. [SQL SERVER系列]读书笔记之SQL注入漏洞和SQL调优

    最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“'1'='1'”这个表达式永远返回 true, ...

  4. Linq To Sql进阶系列(六)用object的动态查询与保存log篇

    动态的生成sql语句,根据不同的条件构造不同的where字句,是拼接sql 字符串的好处.而Linq的推出,是为了弥补编程中的 Data != Object 的问题.我们又该如何实现用object的动 ...

  5. SQL进阶系列之12SQL编程方法

    写在前面 KISS -- keep it sweet and simple 表的设计 注意命名的意义 英文字母 + 阿拉伯数字 + 下划线"_" 属性和列 编程的方针 写注释 注意 ...

  6. SQL进阶系列之10HAVING子句又回来了

    写在前面 HAVING子句的处理对象是集合而不是记录 各队,全队点名 --各队,全体点名! CREATE TABLE Teams (member CHAR(12) NOT NULL PRIMARY K ...

  7. SQL进阶系列之8EXISTS谓词的用法

    写在前面 支撑SQL和关系数据库的基础理论:数学领域的集合论和逻辑学标准体系的谓词逻辑 理论篇 什么是谓词?谓词是返回值为真值(true false unknown)的函数 关系数据库里,每一个行数据 ...

  8. SQL进阶系列之6用关联子查询比较行与行

    写在前面 使用SQL对同一行数据进行列间的比较很简单,只需要在WHERE子句里写上比较条件就可以了,对于不同行数据进行列间比较需要使用自关联子查询. 增长.减少.维持现状 需要用到行间比较的经典场景是 ...

  9. SQL进阶系列之5外连接的用法

    写在前面 SQL本身是作为一种数据提取工具而出现,使用SQL生成各种定制化报表和非定制化报表并非SQL原本用途的功能,但这并不意味着SQL无法实现这些功能. 用外连接进行行列转换(1)(行 → 列): ...

随机推荐

  1. 使用mwget代替wget

    wget相对于浏览器来说,速度会比较偏慢,特别是国外的网站. 刚好经常用的xftp无法正常使用了,于是开始折腾mwget 下面来看一下安装步骤: wget http://jaist.dl.source ...

  2. Putty——支持Telnet,ssh连接的连接软件

    简介 PuTTY 的官方网站:http://www.chiark.greenend.org.uk/~sgtatham/putty/,截止到 2007年6月,发布的最高稳定版本是 0.6.PuTTY 是 ...

  3. 【转】深入理解javascript中的立即执行函数(function(){…})()

    javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...

  4. 关于UiAutomator无法识别的元素

    1.关于没有name,没有ID的元素的定位---通用篇解题思路:因为没有name,id:其实剩下的选择已不多,要么xpath,要么className.xpath木有好印象(稳定性不高,加之1.0x后需 ...

  5. tomcat参数调整

    (1)maxConnections能接收的最大请求数(nio下默认1W,基本不用调整,足够了): (2)acceptCount在操作系统层面的请求堆积,linux操作系统下除了acceptCount参 ...

  6. 【笔试题】Java final keyword

    Java 知识测试 Java final keyword Question 1 What is the use of final keyword in Java? A. When a class is ...

  7. 【转帖】HBase读写的几种方式(二)spark篇

    HBase读写的几种方式(二)spark篇 https://www.cnblogs.com/swordfall/p/10517177.html 分类: HBase undefined 1. HBase ...

  8. 数论 - 同余 + BFS (Find The Multiple)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16995   Accepted: 692 ...

  9. Python之路【第二十四篇】:数据库索引

    数据库索引 一.索引简介 索引在mysql中也叫做"键",是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈 ...

  10. Docker之Alpine制作镜像且上传至阿里云

    目的: Alpine制作jdk镜像 Alpine制作jre镜像(瘦身) Docker镜像上传至阿里云 Alpine制作jdk镜像 alpine Linux简介 Alpine Linux是一个轻型Lin ...