Oracle语法 及 SQL题目(二)
课例复制
思考题四
最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理。
到位后,你发现你的销售数据库中有两张表,一个是商店促销时间的日历,另一个是在促销期间的销售额列表。
你需要编写一个查询,告诉我们在每次促销中哪位职员的销售额最高,这样可以给那个职员发绩效奖金。
-- 商店促销时间的日历
create table promotions (
promo_name varchar2(50) not null primary key, -- 促销活动名称
start_date date not null, -- 开始时间
end_date date not null, -- 终止时间
check(start_date<=end_date)
);
-- 促销期间的销售额表 (注意:该表只是保存促销期间的销售额)
create table sales
(
ticket_nbr int not null primary key, --销售票据编号 (自增)
clerk_name varchar2(20) not null, --销售员姓名
sale_date date not null, --销售日期
sale_amount number(9,2) not null --销售金额
);
insert into promotions values('spring sales',to_date('2009-2-1','yyyy/mm/dd'),to_date('2009-2-15','yyyy/mm/dd'));
insert into promotions values('worker sale',to_date('2009-5-1','yyyy/mm/dd'),to_date('2009-5-4','yyyy/mm/dd'));
insert into promotions values('children sale',to_date('2009-6-1','yyyy/mm/dd'),to_date('2009-6-1','yyyy/mm/dd'));
insert into promotions values('national day sale',to_date('2009-10-1','yyyy/mm/dd'),to_date('2009-10-7','yyyy/mm/dd'));
create sequence seq_nbr;
insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-1','yyyy/mm/dd'),30);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-1','yyyy/mm/dd'),73);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-1','yyyy/mm/dd'),110);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-2','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-2','yyyy/mm/dd'),30);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-2','yyyy/mm/dd'),92);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-3','yyyy/mm/dd'),130);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-3','yyyy/mm/dd'),90);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-3','yyyy/mm/dd'),110);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-4','yyyy/mm/dd'),70);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-4','yyyy/mm/dd'),9);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-5','yyyy/mm/dd'),88);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-5','yyyy/mm/dd'),70);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-2-13','yyyy/mm/dd'),50);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-13','yyyy/mm/dd'),170);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-2-14','yyyy/mm/dd'),270);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-2-15','yyyy/mm/dd'),67.5);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-5-1','yyyy/mm/dd'),280.5);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-1','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-1','yyyy/mm/dd'),113);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-5-2','yyyy/mm/dd'),88);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-2','yyyy/mm/dd'),35.5);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-2','yyyy/mm/dd'),125);
insert into sales values(seq_nbr.nextval,'david', to_date('2009-5-3','yyyy/mm/dd'),92);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-3','yyyy/mm/dd'),93);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-3','yyyy/mm/dd'),167);
insert into sales values(seq_nbr.nextval,'david', to_date('2009-5-4','yyyy/mm/dd'),123.5);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-5-4','yyyy/mm/dd'),200);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-5-4','yyyy/mm/dd'),2);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-6-1','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-6-1','yyyy/mm/dd'),110.5);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-6-1','yyyy/mm/dd'),213);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-1','yyyy/mm/dd'),1123);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-1','yyyy/mm/dd'),780);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-1','yyyy/mm/dd'),310);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-2','yyyy/mm/dd'),139);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-2','yyyy/mm/dd'),1110.5);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-2','yyyy/mm/dd'),998);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-3','yyyy/mm/dd'),120);
insert into sales values(seq_nbr.nextval,'mary',to_date('2009-10-4','yyyy/mm/dd'),10);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-4','yyyy/mm/dd'),234);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-5','yyyy/mm/dd'),110.5);
insert into sales values(seq_nbr.nextval,'tom',to_date('2009-10-6','yyyy/mm/dd'),23);
insert into sales values(seq_nbr.nextval,'david',to_date('2009-10-7','yyyy/mm/dd'),10.5);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-1','yyyy/mm/dd'),30);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-1','yyyy/mm/dd'),73);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-1','yyyy/mm/dd'),110);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-2','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-2','yyyy/mm/dd'),30);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-2','yyyy/mm/dd'),92);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-3','yyyy/mm/dd'),130);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-3','yyyy/mm/dd'),90);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-3','yyyy/mm/dd'),110);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-4','yyyy/mm/dd'),70);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-4','yyyy/mm/dd'),9);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-5','yyyy/mm/dd'),88);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-5','yyyy/mm/dd'),70);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-2-13','yyyy/mm/dd'),50);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-13','yyyy/mm/dd'),170);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-2-14','yyyy/mm/dd'),270);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-2-15','yyyy/mm/dd'),67.5);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-5-1','yyyy/mm/dd'),280.5);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-1','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-5-1','yyyy/mm/dd'),113);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-5-2','yyyy/mm/dd'),88);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-2','yyyy/mm/dd'),35.5);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-5-2','yyyy/mm/dd'),125);
insert into sales values(seq_nbr.nextval,'王海', to_date('2009-5-3','yyyy/mm/dd'),92);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-3','yyyy/mm/dd'),93);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-5-3','yyyy/mm/dd'),167);
insert into sales values(seq_nbr.nextval,'王海', to_date('2009-5-4','yyyy/mm/dd'),123.5);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-5-4','yyyy/mm/dd'),200);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-5-4','yyyy/mm/dd'),2);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-6-1','yyyy/mm/dd'),190);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-6-1','yyyy/mm/dd'),110.5);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-6-1','yyyy/mm/dd'),213);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-10-1','yyyy/mm/dd'),1123);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-1','yyyy/mm/dd'),780);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-1','yyyy/mm/dd'),310);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-2','yyyy/mm/dd'),139);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-2','yyyy/mm/dd'),1110.5);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-10-2','yyyy/mm/dd'),998);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-3','yyyy/mm/dd'),120);
insert into sales values(seq_nbr.nextval,'高春梅',to_date('2009-10-4','yyyy/mm/dd'),10);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-10-4','yyyy/mm/dd'),234);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-5','yyyy/mm/dd'),110.5);
insert into sales values(seq_nbr.nextval,'刘万理',to_date('2009-10-6','yyyy/mm/dd'),23);
insert into sales values(seq_nbr.nextval,'王海',to_date('2009-10-7','yyyy/mm/dd'),10.5);
找出在各次促销活动中,销售量最高的销售员。
请编制一条SQL来完成这个查询。(尽量考虑多种写法)
解题思路
select p.promo_name,s.clerk_name,sum(s.sale_amount) top_sale
from promotions p inner join sales s
on s.sale_date between p.start_date and p.end_date
group by p.promo_name,s.clerk_name,p.start_date,p.end_date
having sum(s.sale_amount) >= all(
select sum(s2.sale_amount)
from sales s2
where s.clerk_name <> s2.clerk_name and s2.sale_date between p.start_date and p.end_date
group by s2.clerk_name
);
SELECT s1.clerk_name,p.promo_name,p.start_date,p.end_date,SUM(s1.sale_amount)
FROM sales s1
INNER JOIN promotions p
ON s1.sale_date BETWEEN p.start_date AND p.end_date
group by s1.clerk_name,p.promo_name,p.start_date,p.end_date
HAVING SUM(s1.sale_amount)>= ALL
(
SELECT SUM(s2.sale_amount)
FROM sales s2
WHERE s2.sale_date BETWEEN p.start_date AND p.end_date
GROUP BY s2.clerk_name
);
思考题五
财务咨询顾问的收入统计问题
金太阳财务咨询服务公司,聘请了一些财务顾问提供财务操作咨询服务,这些顾问都以兼职形式为公司服务,公司将按小时为顾问工作计算薪酬。
具体表信息如下:
a. 顾问信息表 (consultant)
create table consultant(
cst_id int primary key,
cst_name varchar2(30) not null
);
insert into consultant values(1, 'david');
insert into consultant values(2, 'henry');
insert into consultant values(3, 'mary');
insert into consultant values(4, 'kent');
b. 顾问费率表 (billings)
不同级别的顾问,在不同的时期,每小时的收费是不同的,公司会根据情况上调或者下调顾问的小时佣金待遇。
create table billings(
cst_id int not null,
bill_date date not null,
bill_rate int not null,
constraint PK_BILLING PRIMARY KEY(cst_id,bill_date),
constraint FK_BILLING_EMP FOREIGN KEY (cst_id) references consultant(cst_id)
);
insert into billings values(1, to_date('2010-1-1' ,'yyyy/mm/dd'),50);
insert into billings values(2, to_date('2010-1-1' ,'yyyy/mm/dd'),60);
insert into billings values(3, to_date('2010-1-1' ,'yyyy/mm/dd'),70);
insert into billings values(4, to_date('2010-1-1' ,'yyyy/mm/dd'),40);
insert into billings values(1, to_date('2011-1-1' ,'yyyy/mm/dd'),60);
insert into billings values(4, to_date('2011-1-1' ,'yyyy/mm/dd'),45);
c. 顾问工作情况记录表 (hoursworked)
create table hoursworked(
list_id int primary key,
cst_id int not null,
work_date date not null,
bill_hrs decimal(5,2) not null,
constraint FK_HW_EMP FOREIGN KEY (cst_id) references consultant(cst_id)
);
insert into hoursworked values(1,1, to_date('2010-7-1','yyyy/mm/dd'),3);
insert into hoursworked values(2,1, to_date('2010-8-1','yyyy/mm/dd'),5);
insert into hoursworked values(3,2, to_date('2010-7-1','yyyy/mm/dd'),2);
insert into hoursworked values(4,1, to_date('2011-7-1','yyyy/mm/dd'),4);
insert into hoursworked values(5,3, to_date('2011-8-1','yyyy/mm/dd'),3.5);
insert into hoursworked values(6,4, to_date('2010-9-1','yyyy/mm/dd'),10);
insert into hoursworked values(7,4, to_date('2011-8-1','yyyy/mm/dd'),6);
求解:
我们需要你编写一个SQL查询语句,显示顾问的名字以及其总的顾问费用。
解题思路
select c.cst_id,e.cst_name,sum(d.bill_rate*c.bill_hrs) total_fee
from
(
select h.cst_id,h.work_date,max(b.bill_date) bill_date, h.bill_hrs
from billings b inner join hoursworked h
on h.cst_id=b.cst_id and h.work_date>=b.bill_date
group by h.cst_id,h.work_date,h.bill_hrs
)c,billings d,consultant e
where c.cst_id=d.cst_id and c.bill_date=d.bill_date and c.cst_id=e.cst_id
group by c.cst_id,e.cst_name
order by c.cst_id;
Oracle语法 及 SQL题目(二)的更多相关文章
- Oracle语法 及 SQL题目(一)
目录 课例复制 SQL题目一 SQL题目二 SQL题目三 笔记 课例复制 OCM 全称:Oracle Certified Master 认证大师 含义:Oracle 原厂推出的数据库方向最高级别认证 ...
- Oracle语法 及 SQL题目(三)
目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...
- oracle PL、SQL(二)
oracle PL.SQL(基础知识点二) --1,参数 in:表示输入类型,可以省略 :out:输出类型不能省略---------- ----案例1:编写一个过程,可以输入雇员的编号,返回该雇员的姓 ...
- Oracle中PL/SQL简介、基本语法以及数据类型
Oracle中PL/SQL简介.基本语法以及数据类型 一.PL/SQL简介. Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询和Oracle自身过程控 ...
- 【SQL】Oracle的PL/SQL语法及其拓展数据类型总结
PL/SQL语法 PL/SQL程序由三部分组成,声明部分.执行部分.异常处理部分. 模板: DECLARE /*变量声明*/ BEGIN /*程序主体*/ EXCEPTION /*异常处理部分*/ E ...
- ansi sql 语法 切换为 oracle 语法
语句粘贴到 工作表 打开查询构建器 勾选 创建oracle连接 over sql dev 的语法设置调整,否则表别名会右对齐 下面是 转换后的结果,是不是看得舒服多了
- SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻
http://blog.csdn.net/weiwenhp/article/details/8094739 目录(?)[-] SQL Server权限管理 login 与user的区别 角色role ...
- Oracle DBA常用SQL
监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...
- oracle中动态SQL详解
部分内容参考网上资料 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情 ...
随机推荐
- DataPipeline的增量数据支持回滚功能
DataPipeline的增量数据支持回滚功能 第一步:数据任务有增量数据时,回滚按钮激活,允许用户使用该功能进行数据回滚. 第二步:点击回滚按钮,允许用户选择回滚时间或者回滚位置进行数据回滚.选择按 ...
- AOP初识
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理.缓存.对象池管理以及日志记录.AOP将这些分散在各 ...
- 【sqoop】安装配置测试sqoop1
3.1.1 下载sqoop1:sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz 3.1.2 解压并查看目录: [hadoop@hadoop01 ~]$ tar -zxvf sq ...
- 【转】基于TMS320C6455的千兆以太网设计
基于TI公司最新DSP芯片TMS320C6455.设计并实现了以太网通信软硬件接口.采用TMS320C6455片内以太网接口模块EMAC/MDIO,结合片外AR8031 PHY芯片,在嵌入式操作系统D ...
- SCADA系统构架的安全分析总结
概念: SCADA 数据采集与监控 Supervisory Control And Data Acquisition .包括了计算机设备 工业控制组件 网络 组成部件 ,SCADA 系统被用 ...
- python中 "is"和"=="的区别
python中"is"和"=="区别 在做leetcode的时候,在判断两个数据是否相等时使用了python中的is not,想着入乡随俗,既然入了python ...
- java请求url可以带参数
/** * * @param urlStr * url * @param content * 提交的参数 * @param encoding * 编码格式 * @return */ public st ...
- 【Java】《Java程序设计基础教程》第四章学习
4.1 类的封装 封装指的是将东西包装在一起,然后以新的完整形式呈现.包含两个意义: 1). 把对象的全部属性和方法结合在一起,形成一个不可分割的独立单位(即对象). 2). 信息隐藏,即尽可能隐藏对 ...
- CH6803 导弹防御塔
6803 导弹防御塔 0x60「图论」例题 背景 Freda的城堡-- "Freda,城堡外发现了一些入侵者!" "喵...刚刚探究完了城堡建设的方案数,我要歇一会儿嘛l ...
- C++面向对象程序设计第三章习题答案解析
整理一下自己写的作业,供考试前复习用,哈哈 进入正题!!! 题目: 2.分析下面的程序,写出其运行时的输出结果 这里就不展示课本源代码,直接给出修改后的代码,错误部分代码已给出具体的注释 #inclu ...