课例复制

思考题四

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

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

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

-- 商店促销时间的日历
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. Android Room 使用案例

    本内容主要介绍 Android 中使用 Room 保存数据到本地数据库的方法.  以下是 Android Room 的官方介绍文档: Room Persistence Library(Room 库的简 ...

  2. UCOSIII等待多个内核对象

    内核对象 内核对象包括信号量.互斥信号量.消息队列和事件标志组 UCOSIII中允许任务同时等待多个信号量和多个消息队列 主结构体 typedef struct os_pend_data OS_PEN ...

  3. TypeScript_基础数据类型

    TypeScript 的基础数据类型包含: string.number.boolean.array .object.null.undefined.enmu.void.never.any.tuple 注 ...

  4. 二十三、mysql索引管理详解

    一.索引分类 分为聚集索引和非聚集索引. 聚集索引 每个表有且一定会有一个聚集索引,整个表的数据存储在聚集索引中,mysql索引是采用B+树结构保存在文件中,叶子节点存储主键的值以及对应记录的数据,非 ...

  5. Android笔记(五十五) Android四大组件之一——ContentProvider,使用系统提供的ContentProvider

    因为在Android中,存储系统联系人姓名和电话是存在与不同的ContentProvider中的,具体如何查找,可以从Android的源代码中查看,在android.providers包中列出了所有系 ...

  6. Nginx配置文件示例

    Nginx的配置文件示例:(仅供参考) 强烈建议先将默认的配置文件备份再进行操作! 请根据自己项目的实际路径来配置相关路径! uwsgi配置文件请参考:uwsgi配置文件示例 # For more i ...

  7. 开发指南~小程序代码构成~JSON配置

    2.1 JSON 配置 JSON 是一种数据格式,并不是编程语言,在小程序中,JSON扮演的静态配置的角色. 2.1.1 一个例子 ​先看一个例子,打开开发工具的编辑器,在根目录下可以找到 app.j ...

  8. 用指针形式实现strstr函数

    char * mystrstr(char *dest,char * src){ char *p=null; char * temp=src; while(*dest)//只要不为'\0'就行 { p= ...

  9. 0029redis单机版环境搭建

    linux环境下安装单机版redis,主要分为如下几步: 1. 安装gcc 2.下载安装包 3.解压安装包 4.进入解压目录并执行make和make install命令 5.查看默认安装目录 6.更改 ...

  10. memset()函数的使用

    1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...