能否用Sql语句实现这样的功能??

sandy2001 2003-05-09 10:42:44
有一个表
STCDT char(5)
YMDHM DateTime
DYRN number

能否用SQL得到连续3条纪录使得DYRN相加最大的那三条纪录??
不知道我说明白没有??
...全文
61 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckhambobo 2003-05-09
  • 打赏
  • 举报
回复
SQL> select id from aa;

ID
--
1
2
3
4
5
6
6

7 rows selected

SQL> select id,sum(id) over(order by id rows 2 preceding) num from aa;

ID NUM
-- ----------
1 1
2 3
3 6
4 9
5 12
6 15
6 17

7 rows selected

统计记录本行以及前两行总和。
等等,正测试找出三条记录.
developer2002 2003-05-09
  • 打赏
  • 举报
回复
最后的三个编号就是结果
developer2002 2003-05-09
  • 打赏
  • 举报
回复
试试吧,没经过测试,只是想想看,可以这么写。注:需要8i支持。
select stcdt,lag(stcdt,1) over(order by ymdhm) as stcdt_1,lag(stcdt,2) over(order by ymdhm) as stcdt_2,
,max(sum(DYRN) over (order by YMDHM rows 3 preceding)) as result
from 表
group by stcdt,lag(stcdt,1) over(order by ymdhm) as stcdt_1,lag(stcdt,2) over(order by ymdhm) as stcdt_2
blackest 2003-05-09
  • 打赏
  • 举报
回复
写三个UNION,每次选择除了最大的哪个之外最大的记录
developer2002 2003-05-09
  • 打赏
  • 举报
回复
同意楼上
sandy2001 2003-05-09
  • 打赏
  • 举报
回复
是的,根据时间 order by YMDHM
Lastdrop 2003-05-09
  • 打赏
  • 举报
回复
所谓的连续是根据什么呢,时间吗?
qfsb_p 2003-05-09
  • 打赏
  • 举报
回复
用一个sql语句好像不能实现,因为里面包含了过程控制
sandy2001 2003-05-09
  • 打赏
  • 举报
回复
呵呵,我也是这麽想的,可是觉得麻烦,不知道有没有简单一点的方法
maohaisheng 2003-05-09
  • 打赏
  • 举报
回复
写过程,使用游标
developer2002 2003-05-09
  • 打赏
  • 举报
回复
再换一个,我的sql经测试,有问题。我又换了一种写法,只求效果,不求效率的。
前三列就是结果。试试吧:
select b.stcdt,b.stcdt_1,b.stcdt_2,b.result from (
select stcdt,
lag(stcdt,1) over(order by ymdhm) as stcdt_1,
lag(stcdt,2) over(order by ymdhm) as stcdt_2,
sum(DYRN) over (order by YMDHM rows between 1 preceding and 1 following) as result
from 表) b
where b.result= (
select max(a.result) from (
select stcdt,
lag(stcdt,1) over(order by ymdhm) as stcdt_1,
lag(stcdt,2) over(order by ymdhm) as stcdt_2,
sum(DYRN) over (order by YMDHM rows between 1 preceding and 1 following) as result
from 表) a)
/
beckhambobo 2003-05-09
  • 打赏
  • 举报
回复
SQL> select lag(id,2,null) over(order by id) id2,lag(id,1,null) over(order by id) id1,id,sum(id) over(order by id rows 2 preceding) num from aa;

ID2 ID1 ID NUM
--- --- -- ----------
1 1
1 2 3
1 2 3 6
2 3 4 9
3 4 5 12
4 5 6 15
5 6 6 17

以上可以得到并行的三条和最大的id号

17,088

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧