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
日期 代号 数量 价格
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
解决方案 »
- 高手,瞧瞧,小问题,但我不会写了,谢谢!
- 如何累计表中某字段的局部数据,是局部数据,不是整个字段?????(帮转)
- 菜鸟求一SQL语句,比较2个表中的时间字段,取大的那一个字段
- oracle存储过程的问题,是新手,不知道哪错了,希望大家给一下指点
- oracle数据库中的SQL查询
- 如何得到一个Oracle数据表的字段信息?急,急
- oracle configuration assistant安装失败,提示invalid block type
- oracle9i升级补丁到9.2.0.8,在运行catpatch.sql 时报错:SP2-0310:unable to open file "/oracle/product/9.2/rdbms/admin/owapatch.sql"。是不是会影响数据库?
- windows 下的备份数据,怎么样在 Linux 下恢复?
- 求助,用sysdba登录提示连接到空闲例程原因
- exists里面会走索引吗
- 一个很难的sql 不会实现 急!!!
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
你的sql根据实际情况改下就可以了