计算价格涨幅(正负表示)并排序

lirenniao 2010-07-28 11:03:59
表 a : 名称 价格 面积 时间
name price Acreage intime

表 b: id name

计算 表b 某一个商品的 每个月的均价(并按照月份排序)是:
select CONVERT(varchar(7),intime,120) ,cast(avg(Price)/avg(Acreage) as decimal(18,0))
from a where name='aaa' Group by CONVERT(varchar(7),intime,120) order by CONVERT(varchar(7),intime,120) desc

现在我想计算一下,表b中每个商品的当前月份均价 和涨幅度(这感觉挺麻烦。最新有数据的月份对比上一个有数据的月份计算(a-b/b)(涨了是正(比如 0.44)降了是负(比如 -2.34)))

紧急求救。。。
...全文
249 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
lirenniao 2010-08-05
  • 打赏
  • 举报
回复
放弃这个问题了,,,老板说我不适合编程,88了。
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
我试一下, 看看
永生天地 2010-07-28
  • 打赏
  • 举报
回复
(case when t2.av is not null then (t1.av-t2.av)/t2.av end) as av1

不好意思,写错了,应该是not null
[Quote=引用 11 楼 lirenniao 的回复:]
xys_777 的结果是 :(涨幅 貌似不太对。)

2009-07 0 NULL
2010-03 1 NULL
2010-04 0 NULL
2010-05 0 NULL
2010-06 1 NULL
2010-07 0 NULL

不过 我不是要 某一个商品的 ,是所有商品的 均价 和涨幅。
[/Quote]
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
谢谢楼上的热心支持,提供测试数据。。。。。
hokor 2010-07-28
  • 打赏
  • 举报
回复
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'
hokor 2010-07-28
  • 打赏
  • 举报
回复
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'
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
最好把 价格 变动一下。

最后的结果 大概就是
(最近有数据的一个月) 最近有数据的两个月对比计算(涨-正数 跌-负数)
名称 均价 涨幅
cc 2134 1.34%
bb 1567 -0.45%
aa 3452 -1.56
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
测试 数据。。。比较多。。

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',2345,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',2345,120,'2009-9-30 11:24:00');

insert into a (tname,price,Acreage,intime)
values ('bb',2345,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',2345,120,'2009-4-30 11:24:00');

insert into a (tname,price,Acreage,intime)
values ('aa',2345,120,'2009-6-30 11:24:00');

insert into a (tname,price,Acreage,intime)
values ('cc',2345,120,'2009-7-30 11:24:00');

insert into a (tname,price,Acreage,intime)
values ('cc',2345,120,'2009-9-30 11:24:00');

pt1314917 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lirenniao 的回复:]
xys_777 的结果是 :(涨幅 貌似不太对。)

2009-07 0 NULL
2010-03 1 NULL
2010-04 0 NULL
2010-05 0 NULL
2010-06 1 NULL
2010-07 0 NULL

不过 我不是要 某一个商品的 ,是所有商品的 均价 和涨幅。
[/Quote]
那你就弄点测试数据,根据测试数据给出结果。。让别人也好来帮助你。。。否则都只能猜。写了语句,没环境测试。那有什么用?
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
xys_777 的结果是 :(涨幅 貌似不太对。)

2009-07 0 NULL
2010-03 1 NULL
2010-04 0 NULL
2010-05 0 NULL
2010-06 1 NULL
2010-07 0 NULL

不过 我不是要 某一个商品的 ,是所有商品的 均价 和涨幅。
hokor 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lirenniao 的回复:]
最后的 结果应该是 (按照涨幅度排序)

aa 2313 1.33
bb 3456 -1.22
cc 2345 -2.45
[/Quote]
什么数据算出来的结果。。。楼主给点 测试数据吧。。
mingyue074204 2010-07-28
  • 打赏
  • 举报
回复
gr gwrt w
hokor 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pt1314917 的回复:]
SQL code

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)……
[/Quote]
如果是连续每个月都有数据的好,应该没问题,
但楼主说:“最新有数据的月份对比上一个有数据的月份计算(a-b/b)”,上一个有数据的月份不一定是上个月,也可能是上上上个月。。。有点麻烦。。。
lirenniao 2010-07-28
  • 打赏
  • 举报
回复
最后的 结果应该是 (按照涨幅度排序)

aa 2313 1.33
bb 3456 -1.22
cc 2345 -2.45
永生天地 2010-07-28
  • 打赏
  • 举报
回复
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

试试
pt1314917 2010-07-28
  • 打赏
  • 举报
回复

--防止出现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
pt1314917 2010-07-28
  • 打赏
  • 举报
回复

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
永生天地 2010-07-28
  • 打赏
  • 举报
回复
写子查询或者左连接,左连接会好些
pt1314917 2010-07-28
  • 打赏
  • 举报
回复
给点测试数据。。。
pt1314917 2010-07-28
  • 打赏
  • 举报
回复

--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
加载更多回复(1)

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧