【发布时间】:2023-02-22 17:43:19
【问题描述】:
我有一个 tbl_ledger_input 表,其中包含许多我想插入的列 最后是 tbl_ledger_branch 表。但有时列余额值可能为空。例如, 如果4月2日eff_date列的balance值为null,则应从balance中输入 前一天的价值。这个想法是我根据下面的代码将表格与自身连接起来
select
a.ledger_code , a.ref_cur_id , a.ref_branch,a.balance,
b.ledger_code ,b.ref_cur_id, b.ref_branch ,b.balance
from (select * from tbl_ledger_input where eff_date = '06-APR-21' ) a
left join (select * from tbl_ledger_input where eff_date = '07-APR-21') b
on a.ledger_code = b.ledger_code and
a.ref_cur_id = b.ref_cur_id and a.ref_branch = b.ref_branch
where b.ledger_code is null
order by a.ledger_code , a.ref_cur_id ,
a.ref_branch,b.ledger_code ,
b.ref_cur_id, b.ref_branch
,但我只在输出中显示了一天的数据,现在我想为其中的日子编写一个 for 循环 范围, 谢谢你的帮助,请
select
a.ledger_code , a.ref_cur_id , a.ref_branch,a.balance,
NVL(b.ledger_code , a.ledger_code ) ,
NVL(b.ref_cur_id ,a.ref_cur_id ) , NVL(b.ref_branch ,a.ref_branch ) ,
NVL(b.balance , a.balance )
from (select * from tbl_ledger_input where eff_date = '06-APR-21' ) a
left join (select * from tbl_ledger_input where eff_date = '07-APR-21') b
on a.ledger_code = b.ledger_code and a.ref_cur_id = b.ref_cur_id and a.ref_branch =
b.ref_branch
where b.ledger_code is null
order by a.ledger_code , a.ref_cur_id , a.ref_branch,b.ledger_code ,
b.ref_cur_id, b.ref_branch ;
【问题讨论】:
-
请edit问题包括一个minimal reproducible example:
CREATE TABLE表的语句;INSERT一些示例数据的语句(作为文本,而不是图像);您要实现的逻辑的英文描述(不是代码);您的代码的问题/错误的详细信息;以及该样本数据的预期输出。目前您谈论的是日期范围,但您的图像(不使用图像)不显示任何日期,并且您的代码仅使用硬编码字符串,因此不清楚该范围的来源以及您的预期输出会是什么是。 -
SQL 不使用
FOR循环。您可能想要的是行生成器(分层查询或递归查询)来生成日历,然后可能使用PARTITIONedOUTER JOIN或LAG分析函数,但没有问题的更多细节除了猜测之外几乎不可能做任何事情。 -
@astentx 我不被允许编辑