oracle 子查询嵌套问题
我的需求是要查询所有金额等于前次金额的记录。前次记录需要通过日期倒叙来判断。写完的SQL大概是:select*fromCDBCFA_TMP_EXDEBT_ASaaawhe...
我的需求是 要查询所有金额等于前次金额的记录。 前次记录需要通过日期倒叙来判断。
写完的SQL大概是:
select *
from CDBCFA_TMP_EXDEBT_AS aaa
where aaa.bustype = 'AN'
and aaa.accoamount = (select accoamount --金额
from (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc)
where rownum = 1); --并且是最新一条记录
但是报错,aaa找不到,是不是因为多嵌套了一层所以oracle不认?
我这种需求改怎么写,小弟拜谢!! 展开
写完的SQL大概是:
select *
from CDBCFA_TMP_EXDEBT_AS aaa
where aaa.bustype = 'AN'
and aaa.accoamount = (select accoamount --金额
from (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc)
where rownum = 1); --并且是最新一条记录
但是报错,aaa找不到,是不是因为多嵌套了一层所以oracle不认?
我这种需求改怎么写,小弟拜谢!! 展开
2个回答
展开全部
你好:
(1)首先,请确认表名称正确,且“aaa.”及“t1.”后面跟随的表字段都存在
(2)“aaa.accoamount =”这里直接用等于号是不恰当的
可以将“aaa.accoamount”后面的“=”改为“in”运行
(3)再考虑到效率问题,将in用exists替换,可以改为如下语句:
select * from CDBCFA_TMP_EXDEBT_AS aaa where aaa.bustype = 'AN'
and exists (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc
where rownum = 1 --这里请问rownum是属于哪张表中的,请标明后运行
and aaa.accoamount = t1.accoamount);
(1)首先,请确认表名称正确,且“aaa.”及“t1.”后面跟随的表字段都存在
(2)“aaa.accoamount =”这里直接用等于号是不恰当的
可以将“aaa.accoamount”后面的“=”改为“in”运行
(3)再考虑到效率问题,将in用exists替换,可以改为如下语句:
select * from CDBCFA_TMP_EXDEBT_AS aaa where aaa.bustype = 'AN'
and exists (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc
where rownum = 1 --这里请问rownum是属于哪张表中的,请标明后运行
and aaa.accoamount = t1.accoamount);
展开全部
把你没有查rownum怎么可能用rownum = 1 这个条件呢?
select *
from CDBCFA_TMP_EXDEBT_AS aaa
where aaa.bustype = 'AN'
and aaa.accoamount in (select accoamount ,rownum --金额
from (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc)
where rownum = 1);
select *
from CDBCFA_TMP_EXDEBT_AS aaa
where aaa.bustype = 'AN'
and aaa.accoamount in (select accoamount ,rownum --金额
from (select t1.accoamount
from CDBCFA_TMP_EXDEBT_AS t1
where t1.bustype = 'AN'
and t1.chdate < aaa.chdate --前次变动日期需小于这次变动的日期
and t1.exdebtcode = aaa.exdebtcode
order by t1.chdate desc)
where rownum = 1);
来自:求助得到的回答
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询