SQL语句练习实例应用——平均销售等待时间
发布时间:2021-12-25 01:36:05 所属栏目:MsSql教程 来源:互联网
导读:复制代码 代码如下: ---1.平均销售等待时间 ---有一张Sales表,其中有销售日期与顾客两列,现在要求使用一条SQL语句实现计算 --每个顾客的两次购买之间的平均天数 --假设:在同一个人在一天中不会购买两次 create table sales ( custname varchar(10) not n
复制代码 代码如下: ---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 (编辑:爱站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在没有matlabs数据库工具箱的情况下从matlab访问postgr
- sql-server – 什么时候动态端口“动态”?
- insert select与select into 的用法使用步骤
- sql-server-2008 – SQL Server 2008 – 一个表24gb,删除了
- sql-server – 来自sql server的高磁盘I / O还是高磁盘I /
- sql server 2012 FileTable有什么些功能?
- 利用sys.sysprocesses检查SqlServer的阻塞和死锁
- sql-server – 为什么表使用其主键作为自身的外键
- 在sql Server自定义一个用户定义星期代码
- SQLServer Execpt和not in 性能差异
站长推荐
热点阅读