我有以下架构
Invoices
=================
invoice_number
account_id
invoice_amount
invoice_date
status ("Paid","Not Paid")
我正在尝试编写查询以获取包含以下信息的所有发票...
- 发票号码
- 发票日期
- 帐户ID
- 发票金额
- 此帐户 ID 的最后支付发票金额(此行的发票日期之前)
我对最后一项(最后支付的发票金额)有疑问。到目前为止,我已经...
select
inv2.invoice_number,
inv2.invoice_date,
inv2.account_id,
inv2.invoice_amount,
(
select * from (
select inv.invoice_amount
from invoices inv
where inv.account_id = inv2.account_id
and inv.status = 'PAID'
and inv.invoice_date < inv2.invoice_date
order by inv.invoice_date desc
)
where rownum <=1
) as last_paid_amount
from
invoices inv2
我正在尝试重构它以不使用相关子查询,因为我的数据集的大小使得这非常昂贵。如何重构以使用不相关的子查询。这甚至可能吗?
谢谢你