【问题标题】:oracle sql inner join by monthsoracle sql内部连接按月
【发布时间】:2014-07-08 14:40:30
【问题描述】:
我有一个所选帐户的列表和一个帐户记录表(可能在所选列表中,也可能不在所选列表中),跨越 40 个月。我可以选择在表格中至少出现一次的所有选定帐户,但我还想查看所有 40 个月中出现的帐户。就像,我希望做类似于out join partition by 的事情,除了我想要更小的数据集。
我该怎么做?谢谢!
示例:
select distinct table1.acct_no
from table1
inner join selectedAcct
on table1.acct_no = selectedAcct.acct_no
【问题讨论】:
标签:
oracle
inner-join
oracle-sqldeveloper
【解决方案1】:
类似的东西(未编译/测试):
with acct_month_list as
(
-- get list of accts/months with missing months
-- as null
select distinct a.acctno, m.monno
from acctrecs a, mons m
where m.monno = a.monno(+)
)
select a2.acctno
from accounts a2
where not exists ( -- any accounst with a null monno have a missing
-- month
select null
from acct_month_list al
where al.acctno = a2.acctno
and a1.monno is null )
and exists ( -- ignore ones that have no records at all
select null
from acctrecs ar
where ar.acctno = a2.acctno )