复制代码 代码如下:
---找出促销活动中销售额最高的职员
---你刚在一家服装销售公司中找到了一份工作,此时经理要求你根据数据库中的两张表得到促销活动销售额最高的销售员
---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
---找出促销活动中销售额最高的职员
---你刚在一家服装销售公司中找到了一份工作,此时经理要求你根据数据库中的两张表得到促销活动销售额最高的销售员
---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
标签:
SQL语句练习,销售额,最高
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“SQL语句练习实例之四 找出促销活动中销售额最高的职员”评论...
更新日志
2024年11月06日
2024年11月06日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]