一个用于现金,另一个用于阶段,具有以下结构
现金
FileID Cash Date
1 50 03.04.2017
2 100 08.07.2015
3 70 14.09.2018
阶段
FileID Stage Date_of_stage
1 Finished 06.04.2016
1 In Process 08.07.2015
2 Complication 17.08.2018
2 In Process 14.03.2018
尽管我的表有更多行。所以我要加入这两个表,因为我想使用以下选择按阶段将现金分组:
select fileID, date, cash, max(date_of_stage) as max_date
from (select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID
and s.date_of_stage < c.date
) x
group by fileID, date, cash
我只需要max(date_of_stage),因为这对于我们的报告在逻辑上是合理的,而且这也不是问题的一部分。
问题是:当我比较现金表和上述选择项中的现金总额时,与上述现金项相比,我从上述选择项中获得的总金额少了一点表格(现金中的700万和上述选择中的6.9百万)。现在,我尝试使用左联接来识别丢失的记录:
select *
from (select fileID, date, cash
from cash) x
left join
(select fileID, date, cash, max(date_of_stage) as max_date
from (select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID
and s.date_of_stage < c.date
)
group by fileID, date, cash ) y
on x.fileID=y.fileID
and x.date=y.date
and x.cash=y.cash
where y.fileID is null
但是这个左联接没有给出任何内容,所以我无法识别和检查丢失的记录。有提示怎么办?
答案 0 :(得分:0)
通过更改左表尝试以下操作
select x.*
(select fileID, date, cash, max(date_of_stage) as max_date
from (select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID
and s.date_of_stage < c.date
)
group by fileID, date, cash ) x left join
(select fileID, date, cash
from cash) y
on x.fileID=y.fileID
and x.date=y.date
and x.cash=y.cash
where y.fileID is null
答案 1 :(得分:0)
我认为您需要做的是在原始查询中进行左外部联接,而不是内部联接,例如:
WITH cash AS (SELECT 1 fileid, 50 cash, to_date('03/04/2017', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT 2 fileid, 100 cash, to_date('08/07/2015', 'dd/mm/yyyy') dt FROM dual UNION ALL
SELECT 3 fileid, 70 cash, to_date('14/09/2018', 'dd/mm/yyyy') dt FROM dual),
stage AS (SELECT 1 fileid, 'Finished' stage, to_date('06/04/2016', 'dd/mm/yyyy') date_of_stage FROM dual UNION ALL
SELECT 1 fileid, 'In Process' stage, to_date('08/07/2015', 'dd/mm/yyyy') date_of_stage FROM dual UNION ALL
SELECT 2 fileid, 'Complication' stage, to_date('17/08/2018', 'dd/mm/yyyy') date_of_stage FROM dual UNION ALL
SELECT 2 fileid, 'In Process' stage, to_date('14/03/2018', 'dd/mm/yyyy') date_of_stage FROM dual)
SELECT c.fileid,
c.dt,
c.cash,
MAX(s.date_of_stage) max_date
FROM cash c
LEFT OUTER JOIN stage s ON c.fileid = s.fileid AND s.date_of_stage < c.dt
GROUP BY c.fileid,
c.dt,
c.cash;
FILEID DT CASH MAX_DATE
---------- ----------- ---------- -----------
1 03/04/2017 50 06/04/2016
2 08/07/2015 100
3 14/09/2018 70
答案 2 :(得分:0)
这很奇怪。使用您提供的数据,“检查”查询可以正常工作并显示两行。这是dbfiddle demo。
无论如何,如果您只需要附加第二个表中的最大日期,请使用简单的子查询:
select fileID, date_, cash,
(select max(date_of_stage)
from stage s
where fileid = c.fileid and s.date_of_stage < c.date_) as max_date
from cash c