22,294
社区成员
发帖
与我相关
我的任务
分享IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[a]') AND type in (N'U'))
DROP TABLE [a]
create table a
(
tname varchar(50),
price decimal(18, 0),
Acreage decimal(18, 0),
intime datetime
);
insert into a (tname,price,Acreage,intime)
values ('aa',1234,120,'2009-8-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('aa',2345,120,'2009-7-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('aa',3345,120,'2009-9-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('bb',1345,120,'2009-7-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('bb',2345,120,'2009-9-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('aa',3345,120,'2009-4-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('aa',4345,120,'2009-6-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('cc',1345,120,'2009-7-30 11:24:00');
insert into a (tname,price,Acreage,intime)
values ('cc',2345,120,'2009-9-30 11:24:00');
;with t as
(
select row_number() over(PARTITION BY tname order by CONVERT(varchar(7),intime,120)) rn ,tname,CONVERT(varchar(7),intime,120) intime,(AVG(price)/AVG(Acreage)) AVGprice from a group by tname,CONVERT(varchar(7),intime,120))
select a.tname,a.intime,case a.AVGprice-b.AVGprice when 0 then 0 else a.AVGprice/(a.AVGprice-b.AVGprice) end 涨幅 from t a left join t b on a.tname = b.tname where b.rn = a.rn - 1 and a.intime = '2009-09'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[tb]') AND type in (N'U'))
DROP TABLE [tb]
GO
select 'aa' name,100.0 price,5 Acreage,GETDATE() intime into tb UNION ALL
select 'aa',300.0,7,DATEADD(MONTH,-1,GETDATE()) UNION ALL
select 'aa',100.0,10,DATEADD(MONTH,-2,GETDATE()) UNION ALL
select 'bb',451.0,3,GETDATE() UNION ALL
select 'bb',123.0,1,DATEADD(MONTH,-2,GETDATE()) UNION ALL
select 'cc',234.0,2,GETDATE() UNION ALL
select 'cc',614.0,5,DATEADD(MONTH,-3,GETDATE())
;with t as
(
select row_number() over(PARTITION BY NAME order by CONVERT(varchar(7),intime,120)) rn ,name,CONVERT(varchar(7),intime,120) intime,(AVG(price)/AVG(Acreage)) AVGprice from tb group by name,CONVERT(varchar(7),intime,120))
select a.name,a.intime,case b.AVGprice when 0 then null else a.AVGprice/(a.AVGprice-b.AVGprice) end 涨幅 from t a left join t b on a.name = b.name where b.rn = a.rn - 1 AND a.intime = '2010-07'select t1.dt,t1.av,
(case when t2.av is null then (t1.av-t2.av)/t2.av end) as av1
from
(select
CONVERT(varchar(7),intime,120) dt ,
cast(avg(Price)/avg(Acreage) as decimal(18,0)) av
from a
where name='aaa'
Group by CONVERT(varchar(7),intime,120) ) t1
left join
(select
CONVERT(varchar(7),intime,120) dt ,
cast(avg(Price)/avg(Acreage) as decimal(18,0)) av
from a
where name='aaa'
Group by CONVERT(varchar(7),intime,120) ) t2
on datediff(month,t2.dt+'-01',t1.dt+'-01')=1
--防止出现null值;
select date,本月均价=本月,涨幅度=isnull(本月,0)-isnull(上月,0) from
(select date=CONVERT(varchar(7),intime,120),本月=cast(avg(Price)/avg(Acreage) as decimal(18,0)),
上月=(select cast(avg(Price)/avg(Acreage) as decimal(18,0)) from a where name=t.name and datediff(mm,intime,t.intime)=1)
from a t where name='aaa'
Group by CONVERT(varchar(7),intime,120))a
order by CONVERT(varchar(7),intime,120) desc
select date,本月均价=本月,涨幅度=本月-上月 from
(select date=CONVERT(varchar(7),intime,120),本月=cast(avg(Price)/avg(Acreage) as decimal(18,0)),
上月=(select cast(avg(Price)/avg(Acreage) as decimal(18,0)) from a where name=t.name and datediff(mm,intime,t.intime)=1)
from a t where name='aaa'
Group by CONVERT(varchar(7),intime,120))a
order by CONVERT(varchar(7),intime,120) desc
--try:
create proc sp_wsp
as
;with wsp
as
(
select tname,date=CONVERT(varchar(7),intime,120),本月=cast(avg(Price)/avg(Acreage) as decimal(18,0)),
px=(select count(1) from a where tname=t.tname and datediff(mm,intime,CONVERT(varchar(7),t.intime,120)+'-01')>=0)
from a t Group by tname,CONVERT(varchar(7),intime,120)
)
select tname,本月均价=本月,涨幅=(本月-(select 本月 from wsp where tname=a.tname and px=a.px-1))*100.0/本月
from wsp a
where px=(select max(px) from wsp where tname=a.tname)
go
--调用
exec sp_wsp