SQL Server SQL语句练习实例之三——平均销售等待时间
2022-11-12 09:46:57
内容摘要
这篇文章主要为大家详细介绍了SQL Server SQL语句练习实例之三——平均销售等待时间,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码
文章正文
这篇文章主要为大家详细介绍了SQL Server SQL语句练习实例之三——平均销售等待时间,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <code> ---1.平均销售等待时间 ---有一张Sales表,其中有销售日期与顾客两列,现在要求使用一条SQL语句实现计算 --每个顾客的两次购买之间的平均天数 --假设:在同一个人在一天中不会购买两次 create table sales ( custname varchar(10) not null, saledate datetime not null ) go insert sales select '张三' , '2010-1-1' union select '张三' , '2010-11-1' union select '张三' , '2011-1-1' union select '王五' , '2010-2-1' union select '王五' , '2010-4-1' union select '李四' , '2010-1-1' union select '李四' , '2010-5-1' union select '李四' , '2010-9-1' union select '李四' , '2011-1-1' union select '赵六' , '2010-1-1' union select '钱途' , '2010-1-1' union select '钱途' , '2011-3-1' union select '张三' , '2011-9-1' go select custname,DATEDIFF(d,min(saledate),max(saledate))/( COUNT (*)-1) as avgday from sales group by custname having count (*)>1 go select custname, case when count (*)>1 then DATEDIFF(d,min(saledate),max(saledate))/( COUNT (*)-1) else DATEDIFF(d,min(saledate),max(saledate)) end as avgday from sales group by custname --having count (*)>1 go drop table sales </code> |
注:关于SQL Server SQL语句练习实例之三——平均销售等待时间的内容就先介绍到这里,更多相关文章的可以留意
代码注释