DDR爱好者之家 Design By 杰米
复制代码 代码如下:
---找出促销活动中销售额最高的职员
---你刚在一家服装销售公司中找到了一份工作,此时经理要求你根据数据库中的两张表得到促销活动销售额最高的销售员
---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
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]