22,210
社区成员
发帖
与我相关
我的任务
分享
-->TravyLee生成测试数据[test]
if object_id('[test]') is not null drop table [test]
create table [test]([year] int,[month] int,[value] int)
insert [test]
select 2010,1,500 union all
select 2010,2,252 union all
select 2010,3,700 union all
select 2010,4,800 union all
select 2010,5,450 union all
select 2010,6,500 union all
select 2010,7,360 union all
select 2010,8,500 union all
select 2010,9,780 union all
select 2010,10,840 union all
select 2010,11,880 union all
select 2010,12,1000 union all
select 2011,1,1400 union all
select 2011,2,574 union all
select 2011,3,700 union all
select 2011,4,447 union all
select 2011,5,1147 union all
select 2011,6,147 union all
select 2011,7,360 union all
select 2011,8,1474 union all
select 2011,9,780 union all
select 2011,10,1474 union all
select 2011,11,880 union all
select 2011,12,541 union all
select 2012,1,500 union all
select 2012,2,780
--更正一下,改一下排序规则
with t
as(
select px=ROW_NUMBER()over(order by getdate()),
* from test
)
select *,isnull((select avg(value) from(
select top 12 value from t b where a.px>b.px order by b.px desc)s),0) as 销量
from t a
/*
px year month value 销量
------------------------------------------
1 2010 1 500 0
2 2010 2 252 500
3 2010 3 700 376
4 2010 4 800 484
5 2010 5 450 563
6 2010 6 500 540
7 2010 7 360 533
8 2010 8 500 508
9 2010 9 780 507
10 2010 10 840 538
11 2010 11 880 568
12 2010 12 1000 596
13 2011 1 1400 630
14 2011 2 574 705
15 2011 3 700 732
16 2011 4 447 732
17 2011 5 1147 702
18 2011 6 147 760
19 2011 7 360 731
20 2011 8 1474 731
21 2011 9 780 812
22 2011 10 1474 812
23 2011 11 880 865
24 2011 12 541 865
25 2012 1 500 827
26 2012 2 780 752
*/
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([year] int,[month] int,[value] int)
insert [test]
select 2010,1,500 union all
select 2010,2,252 union all
select 2010,3,700 union all
select 2010,4,800 union all
select 2010,5,450 union all
select 2010,6,500 union all
select 2010,7,360 union all
select 2010,8,500 union all
select 2010,9,780 union all
select 2010,10,840 union all
select 2010,11,880 union all
select 2010,12,1000 union all
select 2011,1,1400 union all
select 2011,2,574 union all
select 2011,3,700 union all
select 2011,4,447 union all
select 2011,5,1147 union all
select 2011,6,147 union all
select 2011,7,360 union all
select 2011,8,1474 union all
select 2011,9,780 union all
select 2011,10,1474 union all
select 2011,11,880 union all
select 2011,12,541 union all
select 2012,1,500 union all
select 2012,2,780
with t
as(
select px=ROW_NUMBER()over(order by getdate()),
* from test
)
select *,isnull((select avg(value) from(
select top 12 value from t b where a.px>b.px order by b.px)s),0) as 销量
from t a
/*
px year month value 销量
---------------------------------------------
1 2010 1 500 0
2 2010 2 252 500
3 2010 3 700 376
4 2010 4 800 484
5 2010 5 450 563
6 2010 6 500 540
7 2010 7 360 533
8 2010 8 500 508
9 2010 9 780 507
10 2010 10 840 538
11 2010 11 880 568
12 2010 12 1000 596
13 2011 1 1400 630
14 2011 2 574 630
15 2011 3 700 630
16 2011 4 447 630
17 2011 5 1147 630
18 2011 6 147 630
19 2011 7 360 630
20 2011 8 1474 630
21 2011 9 780 630
22 2011 10 1474 630
23 2011 11 880 630
24 2011 12 541 630
25 2012 1 500 630
26 2012 2 780 630
*/