请原谅无法提供原表的建表SQL 和 模拟数据某表
国家-------行业-------总值---------时间---------上次总值
china------农业--------50--------2010-01-25----空缺目前没值
china------农业--------40--------2010-01-15----空缺目前没值
china------农业--------30--------2010-01-21----空缺目前没值
china------农业--------20--------2010-01-05----空缺目前没值
USA--------工业--------50--------2010-01-25----空缺目前没值
USA--------工业--------40--------2010-01-15----空缺目前没值
USA--------玩具--------500-------2010-01-25----空缺目前没值
USA--------玩具--------1000------2010-01-05----空缺目前没值
需要对该表进行更新的操作,但不能用update,最好是用 insert
规则、举例:
国家是中国,行业是农业,时间是2010-1-25 的总值是50,他上次总值是 2010-01-21 号的 30国家是中国,行业是农业,时间是2010-1-15 的总值是40,他上次总值是 2010-01-05 号的 20例外:
国家是中国,行业是农业,时间是2010-1-05 的总值是20,当前表中数据 同行业同国家的, 没有比他日期还小的,那么给以个值 0以上面模拟数据,更新后的结果为:国家-------行业-------总值---------时间---------上次总值
china------农业--------50--------2010-01-25------30
china------农业--------40--------2010-01-15------20
china------农业--------30--------2010-01-21------40
china------农业--------20--------2010-01-05------0
USA--------工业--------50--------2010-01-25------40
USA--------工业--------40--------2010-01-15------0
USA--------玩具--------500-------2010-01-25------1000
USA--------玩具--------1000------2010-01-05------0
就是这样了, 求SQL
国家-------行业-------总值---------时间---------上次总值
china------农业--------50--------2010-01-25----空缺目前没值
china------农业--------40--------2010-01-15----空缺目前没值
china------农业--------30--------2010-01-21----空缺目前没值
china------农业--------20--------2010-01-05----空缺目前没值
USA--------工业--------50--------2010-01-25----空缺目前没值
USA--------工业--------40--------2010-01-15----空缺目前没值
USA--------玩具--------500-------2010-01-25----空缺目前没值
USA--------玩具--------1000------2010-01-05----空缺目前没值
需要对该表进行更新的操作,但不能用update,最好是用 insert
规则、举例:
国家是中国,行业是农业,时间是2010-1-25 的总值是50,他上次总值是 2010-01-21 号的 30国家是中国,行业是农业,时间是2010-1-15 的总值是40,他上次总值是 2010-01-05 号的 20例外:
国家是中国,行业是农业,时间是2010-1-05 的总值是20,当前表中数据 同行业同国家的, 没有比他日期还小的,那么给以个值 0以上面模拟数据,更新后的结果为:国家-------行业-------总值---------时间---------上次总值
china------农业--------50--------2010-01-25------30
china------农业--------40--------2010-01-15------20
china------农业--------30--------2010-01-21------40
china------农业--------20--------2010-01-05------0
USA--------工业--------50--------2010-01-25------40
USA--------工业--------40--------2010-01-15------0
USA--------玩具--------500-------2010-01-25------1000
USA--------玩具--------1000------2010-01-05------0
就是这样了, 求SQL
,行业
,总值
,时间
,lag(总值,1,0) over(partition by 国家,行业 order by 时间 ) 上次总值
from tb
,行业
,总值
,时间
,nvl(lag(总值,1,0) over(partition by 国家,行业 order by 时间 ),0) 上次总值
from tb
已写入 file afiedt.buf 1 with tb as(
2 select 'china' 国家,'农业' 行业,50 总值,
3 to_date('2010-01-25','yyyy-mm-dd') 时间,null 上次总值 from dual
4 union all
5 select 'china' ,'农业',40,to_date('2010-01-15','yyyy-mm-dd'),null from dual
6 union all
7 select 'china' ,'农业',30,to_date('2010-01-21','yyyy-mm-dd'),null from dual
8 union all
9 select 'china' ,'农业',20,to_date('2010-01-05','yyyy-mm-dd'),null from dual
10 union all
11 select 'USA' ,'工业',50,to_date('2010-01-25','yyyy-mm-dd'),null from dual
12 union all
13 select 'USA' ,'工业',40,to_date('2010-01-15','yyyy-mm-dd'),null from dual
14 union all
15 select 'USA' ,'玩具',500,to_date('2010-01-25','yyyy-mm-dd'),null from dual
16 union all
17 select 'USA' ,'玩具',1000,to_date('2010-01-05','yyyy-mm-dd'),null from dual
18 )
19 select 国家,行业,总值,时间,
20 nvl(lag(总值,1,0) over(partition by 国家,行业 order by 时间 ),0) 上次总值
21* from tb
SQL> /国家 行业 总值 时间 上次总值
----- ---- ---------- -------------- ----------
USA 工业 40 15-1月 -10 0
USA 工业 50 25-1月 -10 40
USA 玩具 1000 05-1月 -10 0
USA 玩具 500 25-1月 -10 1000
china 农业 20 05-1月 -10 0
china 农业 40 15-1月 -10 20
china 农业 30 21-1月 -10 40
china 农业 50 25-1月 -10 30已选择8行。