我有两个类似的查询,它们都在同一个表上工作,我基本上想要结合他们的结果,使第二个查询提供第一个查询不返回的默认值.我在这里尽可能地简化了这个问题.我正在使用Oracle btw.
该表中包含许多帐户的帐户信息,每个帐户都有多个条目,其中commit_date用于指示何时插入帐户信息.我需要获取当前特定日期的帐户信息.
查询会记录帐户ID和日期.
这是查询:
-- Select the row which was current for the accounts for the given date. (won't return anything for an account which didn't exist for the given date)
SELECT actr.*
FROM Account_Information actr
WHERE actr.account_id in (30000316, 30000350, 30000351)
AND actr.commit_date <= to_date( '2010-DEC-30','YYYY-MON-DD ')
AND actr.commit_date =
(
SELECT MAX(actrInner.commit_date)
FROM Account_Information actrInner
WHERE actrInner.account_id = actr.account_id
AND actrInner.commit_date <= to_date( '2010-DEC-30','YYYY-MON-DD ')
)
Run Code Online (Sandbox Code Playgroud)
这看起来有点难看,但它为每个帐户返回一行,该行在给定日期是当前的.问题是,如果帐户在给定日期之后才存在,则不会返回任何内容.
为每个帐户选择最早的帐户信息很简单 - 我不需要为此提供日期:
-- Select the earliest row for the …Run Code Online (Sandbox Code Playgroud)