SQL語句演習實例之三——均勻發賣期待時光。本站提示廣大學習愛好者:(SQL語句演習實例之三——均勻發賣期待時光)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL語句演習實例之三——均勻發賣期待時光正文
---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