课例复制

思考题四

最近,经过你的努力,你得到了一份工作,成为了百货公司的一位经理。

到位后,你发现你的销售数据库中有两张表,一个是商店促销时间的日历,另一个是在促销期间的销售额列表。

你需要编写一个查询,告诉我们在每次促销中哪位职员的销售额最高,这样可以给那个职员发绩效奖金。

-- 商店促销时间的日历
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题目(二)的更多相关文章

  1. Oracle语法 及 SQL题目(一)

    目录 课例复制 SQL题目一 SQL题目二 SQL题目三 笔记 课例复制 OCM 全称:Oracle Certified Master 认证大师 含义:Oracle 原厂推出的数据库方向最高级别认证 ...

  2. Oracle语法 及 SQL题目(三)

    目录 SQL题目六 第一个问题思路(查询酒类商品的总点击量) 第二个问题思路(查询每个类别所属商品的总点击量,并按降序排列) 第三个问题思路(查询所有类别中最热门的品种(点击量最高),并按点击量降顺序 ...

  3. oracle PL、SQL(二)

    oracle PL.SQL(基础知识点二) --1,参数 in:表示输入类型,可以省略 :out:输出类型不能省略---------- ----案例1:编写一个过程,可以输入雇员的编号,返回该雇员的姓 ...

  4. Oracle中PL/SQL简介、基本语法以及数据类型

    Oracle中PL/SQL简介.基本语法以及数据类型 一.PL/SQL简介. Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询和Oracle自身过程控 ...

  5. 【SQL】Oracle的PL/SQL语法及其拓展数据类型总结

    PL/SQL语法 PL/SQL程序由三部分组成,声明部分.执行部分.异常处理部分. 模板: DECLARE /*变量声明*/ BEGIN /*程序主体*/ EXCEPTION /*异常处理部分*/ E ...

  6. ansi sql 语法 切换为 oracle 语法

        语句粘贴到 工作表 打开查询构建器 勾选 创建oracle连接 over     sql dev 的语法设置调整,否则表别名会右对齐   下面是 转换后的结果,是不是看得舒服多了

  7. SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻

    http://blog.csdn.net/weiwenhp/article/details/8094739 目录(?)[-] SQL Server权限管理 login 与user的区别 角色role ...

  8. Oracle DBA常用SQL

    监控SQL 1.监控事例的等待: select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,c ...

  9. oracle中动态SQL详解

    部分内容参考网上资料 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情 ...

随机推荐

  1. UI5-技术篇-JS函数eval动态执行

    <html lang="en"> <head> <meta charset="utf-8" /> <title> ...

  2. arm9的中断

    GPIO 习惯了stm32的GPIO,发现高端处理器arm在这方面反而简单了. ARM9控制GPIO只有三种寄存器. GPxCON:配置引脚功能,GPACON用一位控制一个GPIO,分别是0为输出引脚 ...

  3. Java经典逻辑编程50题 (转)

    注: 转自https://blog.csdn.net/Alias_fa/article/details/52985112  (摘了题目部分和部分分析,想看原作代码的见原作链接) [程序1] 題目:古典 ...

  4. sqlserver 汉字转拼音(转载)

    转载来源一:https://www.cnblogs.com/zhuisuo/archive/2012/01/11/2318908.html 汉字转全拼音函数优化方案(SQLServer),值得你看看 ...

  5. SqlServer中-char varchar nvarchar的区别

    说说nvarchar和varchar的区别:的区别: varchar:  可变长度,存储ANSI字符,根据数据长度自动变化. nvarchar: 可变长度,存储Unicode字符,根据数据长度自动变化 ...

  6. Delphi-RzDbgrid-绘制表格式设置某行颜色或者其他格式-以及隔行换色的属性

    参考文章:https://www.cnblogs.com/OSKnown/p/8568740.html 在DbgridEh和原生的Dbgrid直接在DrawColumnCell事件中写重绘代码就好了, ...

  7. Zabbix-报警之微信(Wechat alert)告警

    1.前言 Zabbix告警发送是监控系统的重要功能之一.Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式 ...

  8. Android笔记(五十三) 利用有道OPENAPI做简单的翻译demo

    先去 http://fanyi.youdao.com/openapi?path=data-mode 申请开发者key 有道api会自动将申请的单词翻译并返回为xml或者json格式,我们所需要做的就是 ...

  9. 软件测试_Loadrunner_性能测试_服务器资源监控

    使用Loadrunner进行Windows服务器性能监控 将装有Loadrunner的机器叫做监控端,被监控资源的服务器叫做被监控端 一.前置环境设置 1. 设置被监控端管理员账户可用:我的电脑右键- ...

  10. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...