比较复杂的语句,请大家看看,有关同比,环比的.

生活的信仰 2011-03-16 11:59:46


create table #car
(years int, --年份
months int, --月份
num int, --销售数量
area nvarchar(28) --地域
)
insert into #car
select 2008,1,30,'华中' union all select 2008,2,20,'华南' union all select 2008,12,40,'华北' union all
select 2009,1,15,'华中' union all select 2009,2,50,'华南' union all select 2009,12,70,'华北' union all
select 2010,1,130,'华中' union all select 2010,2,40,'华南' union all select 2010,12,100,'华北'

--写出 years months area num 同比, 环比,累计销量,累计同比,累计环比

--解释: 同比 = (当年当月销量 -去年当月销量)/去年当月销量
-- 环比 = (当年当月销量 -上月销量) / 上月销量
-- 累计销量 = 当年累计销量之和
select area,years,months,num from #car

...全文
150 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-03-16
  • 打赏
  • 举报
回复
好像前不久写过一个类似的...
--小F-- 2011-03-16
  • 打赏
  • 举报
回复
select
isnull(a.num,0)-isnull(b.num,0)/isnull(b.num,0) as 同比,
...
力气活...
silentcross 2011-03-16
  • 打赏
  • 举报
回复
同比,环比类似

Select
(thisyear.num - lastyear.num) / lastyear.num
From
#car lastyear
inner join
#car thisyear
on
lastyear.years = thisyear.years -1 and
lastyear.month = thisyear.month and
lastyear.area = thisyear.area

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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