我很想知道如何在Oracle中基于返回行列表中的上一行编写SQL查询。基本上,我必须查询我们的数据库中所有ID为2460的行,但前一行(按日期排序)的ID号是2463.有没有办法做到这一点?Oracle SQL:根据先前行中的值进行选择列
对不起,如果这是混乱或一个愚蠢的问题,我是非常新的,并不是很擅长这类事情。我很乐意澄清任何需要澄清的事情。
谢谢!
编辑:
下面是我根据戈登与我越来越VS我想要的结果,结果的截图沿着答案运行查询。
select *
from (select activitytranledger.*, lag(reasontype) over (order by trandate) as prev_reason from activitytranledger) activitytranledger
where trandate between to_date('02/7/2017','MM/DD/YYYY')
and to_date('02/8/2017','MM/DD/YYYY')
and reasontype = 2460
and prev_reason = 2463
order by trandate
;
然后,我将找到位置#并查询它的具体日期。
select *
from activitytranledger
where location = 5777
and trandate between to_date('02/7/2017','MM/DD/YYYY')
and to_date('02/8/2017','MM/DD/YYYY')
order by trandate
;
使用“Transid”我可以找到第一个查询输出的特定行。但是,它似乎没有给我想要的结果。
Wanted results(注意行直接与2460“Reasontype”行上面有着怎样的理由类型2463)
Current results(我强调该行应该有2463的,我指着列)
编辑2:切换2463和2460
编辑您的问题,并提供样本数据和预期结果。 –