返回低于给定日期oracle SQL的下一个MAX日期

时间:2014-03-28 21:04:30

标签: sql oracle11g

我有这个问题:

select inv_job.job_id,
       max( bh_invoice_smy.invoice_date) as d_invoice_date
from job

join job inv_job on job.job_id = inv_job.invoice_job_id
join customer on customer.cus_id = inv_job.cus_id
join organization_unit on organization_unit.ou_id = inv_job.ou_id
left outer join bh_invoice_smy on bh_invoice_smy.job_id = job.job_id

where
to_date(bh_invoice_smy.invoice_date,'YYYY/MM/DD') <= to_date( 'INPUTDATE','YYYY/MM/DD')

group by inv_job.job_id

我试图让它返回小于INPUTDATE的最后一个d_invoice_date,目前这只是省略了大于INPUTDATE的d_invoice_date。出于发布目的,我还在where子句中省略了大约15个其他谓词,这些谓词都不会影响d_invoice_date的结果。

我查看过很多关于subquerys和内部联接的帖子,但我似乎无法得到解决方案。

1 个答案:

答案 0 :(得分:0)

像这样(伪代码):

SELECT Dt 
FROM Tbl
WHERE Dt < InputDate AND ROWNUM = 1
ORDER BY Dt DESC