SQL语句练习实例之三——平均销售等待时间

来源:asp之家 时间:2011-10-24 20:11:47 

代码如下:


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

标签:平均销售,等待时间
0
投稿

猜你喜欢

  • 用VBS语言实现的网页计算器源代码

    2007-12-26 17:09:00
  • Vue.js 使用v-cloak后仍显示变量的解决方法

    2024-06-07 15:20:56
  • 必须会的SQL语句(五) NULL数据处理和类型转换

    2024-01-18 23:40:51
  • Golang中panic的异常处理

    2023-10-13 12:27:23
  • 高考考python编程是真的吗

    2023-11-13 04:48:47
  • 让自定义文件下载支持断点续传

    2009-03-11 19:45:00
  • 分享一个可以生成各种进制格式IP的小工具实例代码

    2022-02-19 05:22:37
  • Python3爬虫RedisDump的安装步骤

    2023-07-17 00:42:15
  • Python OpenCV之常用滤波器使用详解

    2023-02-07 22:18:25
  • tesseract-ocr使用以及训练方法

    2022-07-06 17:23:37
  • .NET之生成数据库全流程实现

    2024-01-16 05:08:48
  • Linux下mysql新建账号及权限设置方法

    2024-01-22 21:38:07
  • php使用composer常见问题及解决办法

    2023-07-10 13:54:53
  • 2010怎么就宅了——我们是设计星球的阿凡达

    2010-03-09 13:26:00
  • python实现模拟数字的魔术游戏

    2021-03-21 17:06:39
  • PyTorch 中的 torch.utils.data 解析(推荐)

    2021-09-14 01:58:16
  • python中的装饰器该如何使用

    2021-01-17 20:40:45
  • Python地图四色原理的遗传算法着色实现

    2022-10-07 20:57:45
  • python中Apriori算法实现讲解

    2023-10-27 17:41:20
  • Python模拟简单电梯调度算法示例

    2021-09-27 14:52:31
  • asp之家 网络编程 m.aspxhome.com