求如下要求的估算值
有表tabel A 表示时间,date_id(identity),the_date 为日期,
取每月1号,依次增加。the_year为年号,the_month为月份。
部分如下:
date_id the_date the_year the_month
1 2000-1-1 2000 1
2 2000-2-1 2000 2
3 2000-3-1 2000 3
4 2000-4-1 2000 4
..
11 2000-11-1 2000 11
12 2000-12-1 2000 12
13 2001-1-1 2001 1
..
25 2002-1-1 2002 1
..
37 2003-1-1 2003 1
48 2003-12-1 2003 12
有表tabel B表示数据 id(identity),date_id 为表a 中的date_id ,
查询做内连接的那种。value 表示数字。部分数据如下:
id(identity) date_id value
1 1 3200
2 2 4320
3 3 7600
..
13 13 4100
现在要进行这样的计算,给定一个时间的date_id,要求求出该年十二月份的数据。
如果该时间对应年份有12月份的数据,那么返回该value。
如果该时间对应年份没有12月份的数据,那么返回12月份的估算值。
估算值的计算方法如下:
例如现在2003年只有1到8月的数据,要求12月的数据。
我们需要先计算2003年所有以前8月和12月的比值,2000年8月和12月的比值a2000,
2001年8月和12月的比值a2001,2002年8月和12月的比值a2002。取平均值aavg。
然后用2003年8月的值除以aavg,得到12月的估算值。
这样的存储过程怎么样写?