27,579
社区成员
发帖
与我相关
我的任务
分享
-- 先根据原表统计每种产品,每个月的总销量
;with t as(
select id, datepart(mm,日期) [month], sum(销售量) [sales] from tab group by id, datepart(mm,日期)
)
-- 再将上月的总销量与本月的总销量进行关联,计算出本月相对于上月的增长
select t2.id, t2.[month], cast((t2.[sales]-t1.[sales])*100.0/t2.[sales] as number(10,2))
from t t1 inner join t t2
on t1.id=t2.id and t2.[month]-t1.[month]=1;
-- 没有数据,只能大概写一些了。
-- 月环比增长(id 为产品 id)。
-- 周统计用 datepart(ww,日期)。当然这些数据都应该是同一年的。
;with t as(
select id, datepart(mm,日期) [month], sum(销售量) [sales] from tab group by id, datepart(mm,日期)
)
select t2.id, t2.[month], cast((t2.[sales]-t1.[sales])*100.0/t2.[sales] as number(10,2))
from t t1 inner join t t2
on t1.id=t2.id and t2.[month]-t1.[month]=1;