最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL語句練習(xí)實(shí)例之三——平均銷售等待時(shí)間

 更新時(shí)間:2011年10月07日 00:24:41   作者:  
有一張Sales表,其中有銷售日期與顧客兩列,現(xiàn)在要求使用一條SQL語句實(shí)現(xiàn)計(jì)算
復(fù)制代碼 代碼如下:

---1.平均銷售等待時(shí)間
---有一張Sales表,其中有銷售日期與顧客兩列,現(xiàn)在要求使用一條SQL語句實(shí)現(xiàn)計(jì)算
--每個(gè)顧客的兩次購買之間的平均天數(shù)
--假設(shè):在同一個(gè)人在一天中不會購買兩次
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

相關(guān)文章

最新評論

怀集县| 光泽县| 北京市| 淮滨县| 革吉县| 康保县| 大港区| 怀柔区| 札达县| 遵义市| 宝兴县| 万山特区| 武平县| 临高县| 樟树市| 峨边| 新巴尔虎左旗| 太和县| 涟源市| 昌图县| 芜湖县| 抚顺县| 九江县| 日喀则市| 拉孜县| 崇左市| 甘南县| 嘉善县| 依兰县| 崇左市| 泽州县| 巢湖市| 梨树县| 桂东县| 阿鲁科尔沁旗| 石首市| 陇西县| 小金县| 环江| 新田县| 华蓥市|