我收到错误,我的代码(Oracle)中缺少关键字。我的想法是创建一个as / was视图花费last3,6,12。因此,我想知道jan14花费的最后3个月,过去3个月中包含的月数将是:Oct13,Nov13,Dec14。
使用合并到account_month_tb (选择account_month_tb.acct_id,account_month_tb.month_start_dt,
--rolling spend buckets
sum(spend) over
(partition by account_month_tb.acct_id order by account_month_tb.month_start_dt
range between interval '3' month preceding and preceding month) as spend_last3,
sum(spend) over
(partition by account_month_tb.acct_id order by account_month_tb.month_start_dt
range between interval '6' month preceding and preceding month) as spend_last6,
sum(spend) over
(partition by account_month_tb.acct_id order by account_month_tb.month_start_dt
range between interval '12' month preceding and preceding month) as spend_last12
--mkdn buckets
from account_month_tb
) rolling_14
on (account_month_tb.acct_id = rolling_14.acct_id and
account_month_tb.month_start_dt = rolling_14.month_start_dt)
when matched then update set
account_month_tb.spend_last3 = rolling_14.spend_last3,
account_month_tb.spend_last6 = rolling_14.spend_last6,
account_month_tb.spend_last12 = rolling_14.spend_last12
;
commit;
错误消息:
命令行错误:163列:62
错误报告 -
SQL错误:ORA-00905:缺少关键字
00905. 00000 - "缺少关键字"
*原因:
*行动:
提交。
答案 0 :(得分:0)
在windowing clause内,preceding month
并不代表任何内容,也无效。您的意思是interval '1' month preceding
。
在CTE中模拟了一些虚拟数据:
with account_month_tb as (
select 1 as acct_id,
trunc(sysdate - (16 * level), 'MM') as month_start_dt,
level as spend
from dual
connect by level < 10
)
select account_month_tb.acct_id, account_month_tb.month_start_dt,
sum(spend) over (partition by account_month_tb.acct_id
order by account_month_tb.month_start_dt
range between interval '3' month preceding
and interval '1' month preceding) as spend_last3
from account_month_tb;
ACCT_ID MONTH_START_DT SPEND_LAST3
---------- -------------- -----------
1 01-09-2014
1 01-10-2014 9
1 01-10-2014 9
1 01-11-2014 24
1 01-12-2014 30
1 01-12-2014 30
1 01-01-2015 30
1 01-01-2015 30
1 01-02-2015 20
SQL Fiddle包含三个和更多数据。但是你会得到很多重复,这可能不是你想要的。也许是you just need distinct values。