请大家看看这段sql哪儿有问题,稍有点复杂
tinfo表结果
日期 代号 数量 价格
l_date c_code l_amount en_price
想得到的结果:
查询c_code某一天的前5天(不含当日)的平均数量、前3天的总数量、往前推第3天的价格
要可以支持查询一段日期,c_code也不止一个,我写了下面的sql,查询出来的结果发现有些天是对的,但有些天的数据发现不是想要的结果,
又找不出是哪个地方写错了,不常用分析函数,请大家看看下面的sql哪儿有问题吧,或者有什么更好的写法:
select a.l_date,
a.c_code,
nvl(avg(a.l_amount) over(partition by a.c_code order by a.l_date
range between 6 preceding and 1 preceding),
0) l_avg_deal,
nvl(sum(a.l_amount) over(partition by a.c_code order by a.l_date
range between 4 preceding and 1 preceding),
0) l_total_amount,
nvl(max(a.en_price) over(partition by a.c_code order by a.l_date
range between 4 preceding and 4 preceding),
0) en_price1
from tinfo a
where a.l_date >= 20060517
and a.l_date <= 20060613
[解决办法]
查询c_code某一天的前5天(不含当日)的平均数量?
有难度 不会
帮顶
[解决办法]
你的错误好隐蔽,我分析了语法应该是没有问题的,主要是时间的问题
select a.l_date,a.c_code,a.l_amount,a.en_price,
nvl(avg(a.l_amount) over(partition by a.c_code
order by a.l_date range between 5 preceding and 1 preceding ),0) avg_5price,
nvl(sum(a.l_amount) over(partition by a.c_code
order by a.l_date range between 3 preceding and 1 preceding ),0) sum_3price,
nvl(avg(a.en_price) over(partition by a.c_code
order by a.l_date range between 3 preceding and 3 preceding ),0) en_3price
from t_info a
where a.l_date > trunc(sysdate - 16)
and a.l_date < sysdate