博客
关于我
SQL语句练习实例之三——平均销售等待时间
阅读量:420 次
发布时间:2019-03-06

本文共 921 字,大约阅读时间需要 3 分钟。

---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

转载地址:http://vhmkz.baihongyu.com/

你可能感兴趣的文章
Neo4j安装部署及使用
查看>>
Neo4j电影关系图Cypher
查看>>
Neo4j的安装与使用
查看>>
Neo4j(1):图数据库Neo4j介绍
查看>>
Neo4j(2):环境搭建
查看>>
Neo4j(3):Neo4j Desktop安装
查看>>
Neo4j(4):Neo4j - CQL使用
查看>>
Neo图数据库与python交互
查看>>
NEO改进协议提案1(NEP-1)
查看>>
Neo私链
查看>>
NervanaGPU 项目使用教程
查看>>
Nerves 项目教程
查看>>
nessus快速安装使用指南(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
Nessus漏洞扫描教程之配置Nessus
查看>>
Nest.js 6.0.0 正式版发布,基于 TypeScript 的 Node.js 框架
查看>>
nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML.
查看>>
nestesd exception is java .lang.NoSuchMethodError:com.goolge.common.collect
查看>>
nestJS学习
查看>>
net core 环境部署的坑
查看>>
NET Framework安装失败的麻烦
查看>>