SQL语句练习实例之四 找出促销活动中销售额最高的职员
来源:asp之家 发布时间:2011-11-03 16:47:03
代码如下:
---找出促销活动中销售额最高的职员
---你刚在一家服装销售公司中找到了一份工作,此时经理要求你根据数据库中的两张表得到促销活动销售额最高的销售员
---1.一张是促销活动表
---2.一张是销售客列表
create table Promotions
(
activity nvarchar(30),
sdate datetime,
edate datetime
)
insert Promotions
select '五一促销活动','2011-5-1','2011-5-7'
union
select '十一促销活动','2011-10-1','2011-10-7'
union
select 'OA专场活动','2011-6-1','2011-6-7'
go
create table sales
(
id int not null,
name nvarchar(20),
saledate datetime,
price money
)
go
insert sales
select 1,'王五','2011-5-1',1000 union
select 1,'王五','2011-5-2',2000 union
select 1,'王五','2011-5-3',3000 union
select 1,'王五','2011-5-4',4000 union
select 1,'张三','2011-5-1',1000 union
select 1,'张三','2011-5-3',2000 union
select 1,'张三','2011-5-4',4000 union
select 1,'李四','2011-5-6',1000 union
select 1,'赵六','2011-5-5',1000 union
select 1,'钱七','2011-5-8',1000 union
select 1,'孙五','2011-6-1',1000 union
select 1,'孙五','2011-6-2',2000 union
select 1,'王五','2011-6-3',3000 union
select 1,'孙五','2011-6-4',4000 union
select 1,'张三','2011-6-1',11000 union
select 1,'张三','2011-6-3',20000 union
select 1,'张三','2011-6-4',4000 union
select 1,'李四','2011-6-6',1000 union
select 1,'赵六','2011-6-5',1000 union
select 1,'钱七','2011-6-8',1500 union
select 1,'孙五','2011-10-1',11000 union
select 1,'孙五','2011-10-2',12000 union
select 1,'王五','2011-10-3',9000 union
select 1,'孙五','2011-10-4',4000 union
select 1,'张三','2011-10-1',11000 union
select 1,'张三','2011-10-3',2000 union
select 1,'张三','2011-10-4',4000 union
select 1,'李四','2011-10-6',27000 union
select 1,'赵六','2011-10-5',9000 union
select 1,'钱七','2011-10-8',3000
go
-----我们需要找出在每次的促销活动中,其销售总额大于 等于
---所有其他职员销售额的职员及促销事件。
---说明:谓词a2.name<>a.name将其他职员从子查询合计中排除出去
---------谓词Between 中的子查询确保我们使用了正确的促销日期
--方法一:
select a.name,b.activity,SUM(a.price) as totalprice
from sales a ,Promotions as b
where a.saledate between b.sdate and b.edate
group by a.name,b.activity
having SUM(price)>= all(select SUM(price) from sales a2
where a2.name<>a.name and a2.saledate between
(
select sdate from Promotions as b2 where b2.activity=b.activity
)
and (select edate from Promotions b3
where b3.activity=b.activity)
group by a2.name)
-----------------
---方法二:
---说明: 如果促销活动时间是不重叠的,则promotions表中只有一个主键列,这样在group by
--子句中使用(activity,sdate,edate)将不会改变。但是它将使having子句可以使用sdate和edate
select a.name,b.activity,SUM(a.price) as totalprice
from sales a ,Promotions as b
where a.saledate between b.sdate and b.edate
group by b.activity,b.sdate,b.edate,a.name
having SUM(price)>= all(select SUM(price) from sales a2
where a2.name<>a.name and a2.saledate between
b.sdate
and b.edate
group by a2.name)
go
--方法三:
---使用cte(sql 2005以后的版本)
with clearksTotal(name,activity,totalprice) as
(
select a.name,b.activity,SUM(price)
from sales a ,Promotions b
where a.saledate between b.sdate and b.edate
group by a.name,b.activity
)
select c1.name,c1.activity,c1.totalprice
from clearksTotal c1
where totalprice=(select MAX(c2.totalprice) from clearksTotal c2
where c1.activity=c2.activity)
go
drop table Promotions
go
drop table sales


猜你喜欢
- 发现问题项目需要,需要删除文件夹中的冗余图片。涉及图像文件名的操作,图像文件名存储在list中python list删除元素有remove(
- 在一些不多的数据下载和生成的时候,我们倾向于直接保存为文件,当我们修改某些参数后再一次运行时,之前运行时生成的文件就被覆盖了。为了解决这个问
- SQL Server数据库查询速度慢的原因有很多,常见的有以下几种:1、没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)
- <script language="javascript"> function disableRightCl
- 1、数组a第0个元素(二维数组)下的所有子元素(一维数组)的第一列import numpy as npb=np.arange(24)a=b.
- 我就废话不多说了,直接上代码吧!import torchimport torch.nn as nnfrom torch.autograd i
- Composer的基本使用在项目中使用composer.json在项目中使用composer,你需要有一个composer.json文件,此
- 当使用Tensorflow训练大量深层的神经网络时,我们希望去跟踪神经网络的整个训练过程中的信息,比如迭代的过程中每一层参数是如何变化与分布
- 在官方网站中对as_index有以下介绍:as_index : boolean, default TrueFor aggregated ou
- 代码如下,U我认为对于新手来说最重要的是学会rnn读取数据的格式。# -*- coding: utf-8 -*-""&q
- 1、利用Python中的random模块中的choice方法random.choice()可以从任何序列,比如list列表中,选取一个随机的
- 本文实例讲述了Python实现的自定义多线程多进程类。分享给大家供大家参考,具体如下:最近经常使用到对大量文件进行操作的程序以前每次写的时候
- 首先了解下session的机制客户端(浏览器中) cookie 作为键值,匹配服务器端的一个数据。然后客户端每次在 HTTP 请求里面带着那
- 引言层次聚类是一种构建聚类层次结构的聚类算法。该算法从分配给它们自己的集群的所有数据点开始。然后将两个最近的集群合并到同一个集群中。最后,当
- 什么是科赫曲线科赫曲线是de Rham曲线的特例。给定线段AB,科赫曲线可以由以下步骤生成: 将线段分成三等份(AC,CD,DB) 以CD为
- 代码实例:try: import termios, TERMIOS 1except ImportErro
- 发现pyautocad模块:可以用python控制autocad的包。今天把文档中的重点内容摘录出来,以后绘图、计算大工程量、或者识别施工图
- 上次介绍了Access 导入 MSSQL 2000/2005 数据库工具,不过,在使用这个工具时还是有一些地方需要注意的,我把整个导入过程记
- 模拟浏览器进行网页加载,当requests,urllib无法正常获取网页内容的时候一、声明浏览器对象注意点一,Python文件名或者包名不要
- pygame.mixer是一个用来处理声音的模块,其含义为“混音器”。游戏中对声音的处理一般包括制造声音和播放声音两部分,这里仅学习了播放声