利用Oracle 10g的MODEL SQL进行行间计算
2008-08-27 12:43:57 来源:WEB开发网如果维度不影响规则,也可以只分区,而沿用原来的规则
规则也可以是多个维度不同取值,本例假定不同年份之间比较,比如要求2008年的'10'=2007年的'30'+'31'
SELECTyear,code,
p_id,v1
FROMt603_2
WHEREcodeIN('600001','600002')
MODELRETURNUPDATEDROWS
PARTITIONBY(code)
DIMENSIONBY(p_id,year)
MEASURES(v1)
RULES(
v1['err1',2008]=v1['30',2007]+v1['31',2007]-v1['10',2008])
ORDERBYcode,p_id;
YEARCODEP_IDV1
-------------------------------
2008600001err10
2008600002err11
如果年份很多,每个年份都是和上年比较,这种描述可以用CV()函数简化
SQL>insertintot603_2select'2006'year,code,p_id,v1,v2fromt603_1;
SELECTyear,code,
p_id,v1
FROMt603_2
WHEREcodeIN('600001','600002')
MODELRETURNUPDATEDROWS
PARTITIONBY(code)
DIMENSIONBY(p_id,year)
MEASURES(v1)
RULES(
v1['err1',foryearin(2007,2008)]=v1['30',CV(year)-1]+v1['31',CV(year)-1]-v1['10',CV(year)])
ORDERBYcode,p_id;
YEARCODEP_IDV1
-------------------------------
2007600001err10
2008600001err10
2007600002err11
2008600002err11
如果year是数值类型,还可以用for year from 2007 to 2009 increment 1的语法,如果是其他类型,还可以用在in子句带子查询的办法,
比如for year in (select year from t603_2)。
但需要注意不能采用year in 的语法,year in的语法只能符号引用已经存在的单元格,而v1['err1',x]是新单元格。
单个年份的写法如下:
SELECTyear,code,
p_id,v1
FROMt603_2
WHEREcodeIN('600001','600002')
MODELRETURNUPDATEDROWS
PARTITIONBY(code)
DIMENSIONBY(p_id,year)
MEASURES(v1)
RULES(
v1['err1',2008]=v1['30',CV()]+v1['31',CV()]-v1['10',CV()])
ORDERBYcode,p_id;
YEARCODEP_IDV1
-------------------------------
2008600001err10
2008600002err11
而
SELECTyear,code,
p_id,v1
FROMt603_2
WHEREcodeIN('600001','600002')
MODELRETURNUPDATEDROWS
PARTITIONBY(code)
DIMENSIONBY(p_id,year)
MEASURES(v1)
RULES(
v1['err1',yearin('2008')]=v1['30',CV()]+v1['31',CV()]-v1['10',CV()])
则返回0行
赞助商链接