≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

SQL Server SQL语句练习实例之四 找出促销活动中销售额最高的职员

人气:324 时间:2019-11-28

这篇文章主要为大家详细介绍了SQL Server SQL语句练习实例之四 找出促销活动中销售额最高的职员,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

代码如下:

 
---找出促销活动中销售额最高的职员 
---你刚在一家服装销售公司中找到了一份工作,此时经理要求你根据数据库中的两张表得到促销活动销售额最高的销售员 
---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 

本文来自:http://www.q1010.com/179/8017-0.html

注:关于SQL Server SQL语句练习实例之四 找出促销活动中销售额最高的职员的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:SQL SERVER

您可能感兴趣的文章

  • SQL Server利用sys.sysprocesses检查SqlServer的阻塞和死锁
  • SQLServer 全文检索(full-text)语法
  • SQL Server 2000 升级到 SQLServer 2008 性能之需要注意的地方之一
  • SQL Server 一次性压缩Sqlserver2005中所有库日志的存储过程
  • SQLServer 跨库查询实现方法
  • SQL Server 数据库主键的生成方式小结(sqlserver,mysql)
  • SQL Server使用cmd命令行窗口操作SqlServer的方法
  • SQLServer 2005 实现数据库同步备份 过程-结果-分析
  • SQL Server CREATE FUNCTION sqlserver用户定义函数
  • sqlserver 查询数据库大小的方法
上一篇:SQL Server SQL截取字符串函数实现方法
下一篇:SQL Server 时间为空的处理的简单示例
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。