对sql作业的总结(PostgreSQL的递归查询)
已知条件如下:
CREATE TABLE appointment (
emp_id integer NOT NULL,
jobtitle varchar(128) NOT NULL,
salary decimal(10,2) NOT NULL,
start_date date NOT NULL,
end_date date NULL
);
ALTER TABLE appointment ADD CONSTRAINT pkey_appointment PRIMARY KEY (emp_id, jobtitle, start_date);
ALTER TABLE appointment ADD CONSTRAINT chk_appointment_period CHECK (start_date <= end_date);
插入数据如下:
INSERT INTO appointment VALUES
(1, ’tutor’, 40000, ’2008-01-01’, ’2009-02-01’),
(1, ’tutor’, 42000, ’2009-01-01’, ’2010-09-30’),
(1, ’tutor’, 45000, ’2012-04-01’, ’2013-12-31’),
(1, ’tutor’, 46000, ’2014-01-01’, ’2014-12-31’),
(1, ’lecturer’, 65000, ’2014-06-01’, NULL),
(2, ’librarian’, 35000, ’2014-01-01’, NULL),
(2, ’tutor’, 20000, ’2014-01-01’, NULL),
(3, ’lecturer’, 65000, ’2014-06-01’, ’2015-01-01’);
既:

问题如下:
Write an SQL query which returns for all current employees the start of their current period of continuous employment. That is, we are asking for the oldest date X such that the employee had one or more appointments on every day since X.
then the query should return

Hint: First construct a subquery to compute appointments for current employees that do not overlap with (or are adjacent to) appointments (for the same employee) starting earlier then select for each employee the latest start-date of such appointments.
代码如下:
WITH RECURSIVE start_appointment AS (
SELECT emp_id, start_date
FROM appointment
WHERE end_date IS NULL
UNION
SELECT a.emp_id, a.start_date
FROM appointment a, start_appointment sa
WHERE a.emp_id = sa.emp_id AND
a.end_date >= (sa.start_date - 1) AND
a.start_date <= sa.start_date
)
SELECT emp_id, min(start_date) as start_date
FROM start_appointment
GROUP BY emp_id;
Union用于合并两个或多个 SELECT 语句的结果集, UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
Union 的话只会选取不同的值,添加All的话重复的 不重复的都会选上。
with recursive 是 postgresql 支持的一种写法,既递归查询。
我们现在有两个表,通过null得表a,where条件是一个表的工作截止日期必须比另个表的开始日期大,开始日期还要比下一个表的开始日期要小,这样才能确保是工作时间是连着的,通过递归不断选择我们所需要的数据,最后min一下得到最小值再gruop by 排好。
结果如下:

对sql作业的总结(PostgreSQL的递归查询)的更多相关文章
- [SQL注入2]FROM SQL INJECTION TO SHELL: POSTGRESQL EDITION
FROM SQL INJECTION TO SHELL: POSTGRESQL EDITION 这里先介绍一下POSTGRESQL.这是一款数据库管理系统,与oracle是同类型软件.08年左右的市场 ...
- MS SQL作业Schedule的限制注意事项
最近遇到了一个关于MS SQL作业Schedule下有限制的特殊案例,有一个作业,用户要求执行的时间为:9:30,14:30,16:30, 19:00,于是我设置了两个Schedule,其中一个每 ...
- 查看sql 作业明细及运行记录
--查看作业明细及状态 select j.name 'Job名', j.description '描述', j.ENABLED job_enabled, cast(js.last_run_date a ...
- 如何跑通第一个 SQL 作业
简介: 本文由阿里巴巴技术专家周凯波(宝牛)分享,主要介绍如何跑通第一个SQL. 一.SQL的基本概念 1.SQL 分类 SQL分为四类,分别是数据查询语言(DQL).数据操纵语言(DML).数据定义 ...
- SQL Server 2005中的CTE递归查询得到一棵树
感觉这个CTE递归查询蛮好用的,先举个例子: use City; go create table Tree ( ID int identity(1,1) primary key not null, N ...
- SQL作业的操作全
--定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...
- 常用SQL操作(MySQL或PostgreSQL)与相关数据库概念
本文对常用数据库操作及相关基本概念进行总结:MySQL和PostgreSQL对SQL的支持有所不同,大部分SQL操作还是一样的. 选择要用的数据库(MySQL):use database_name; ...
- [原创]从Oracle和Microsoft Sql Server迁移到PostgreSQL Plus Advanced Server
一.了解PPAS的迁移方式1.在线迁移和离线迁移使用Migration Studio或Migration Toolkit直接向PPAS数据库进行对象定义和数据表中数据的迁移称为在线迁移,生成要迁移对象 ...
- sql 作业+游标 自动备份数据库
前言 昨天有个同事在客户的服务器上面弄数据库,不小心执行了一条 sql 语句 TRUNCATE TABLE xxx 碉堡了吧,数据全没了 - - ,然后就是在网上拼命的搜索关于数据恢复的软件,搞了一 ...
随机推荐
- paypal文档
https://blog.csdn.net/daily886/article/details/73164643?ref=myread.
- twemproxy源码分析
twemproxy是twitter开源的redis/memcached 代理,数据分片提供取模,一致性哈希等手段,维护和后端server的长连接,自动踢除server,恢复server,提供专门的状态 ...
- [控件] GlowView
GlowView 效果 说明 这是本人第二次写辉光view了,这是改进版本 源码 https://github.com/YouXianMing/UI-Component-Collection // / ...
- Linux查看系统当前字符集
常用的命令展示 参考当前环境的字符集 方法一: cat /etc/sysconfig/i18n [ssh客户端工具最好也是utf-8,保持一致] 方法二:echo $LANG 设置当前环境的字符集 方 ...
- win7下使用手动安装composer
假设我们的php放置在D:\php 目录下, 1.添加环境变量,桌面--> 我的电脑右键---->属性 2.点击高级系统设置 3.点击环境变量 4.选择path,在变量值里面追加内容: ...
- 一键安装lnmp1.5
系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian/Deepin/Aliyun/Amazon/Mint Linux发行版 需要5GB以上硬盘剩余空间,MyS ...
- Nginx-基础配置
正文 本文转载自:http://www.ha97.com/5194.html 文章经过我排版和润色再加工,更加易读.实在是了解nignx配置的基础好文章. 正文 定义Nginx运行的用户和用户组 us ...
- NCE3
Lesson1 A puma at large Pumas are large, cat-like animals which are found in America. When reports ...
- 关于layui(layer)子页面获取不到父页面jQuery对象的问题。
如果在使用layui-layer模块过程中,在子页面执行代码: window.parent.$("#id").val() 报错:window.parent.$ is not a f ...
- python subprocess 和 multiprocess选择以及我遇到的坑
The subprocess option: subprocess is 用来执行其他的可执行程序的,即执行外部命令. 他是os.fork() 和 os.execve() 的封装. 他启动的进程不会把 ...