I'm working on a query which has one with clause (view) and this is joined with a table using INNER/OUTER join. The issue is when the table doesn't have data, the join gets failed and no result is returned. However, the view has data.
I've used an INNER join which is failed in the first case. Then I tried with Left Outer Join which also doesn't work. Actually, the main query is quite complex, so I presented the problem in below code snippet. I'm expecting the answer in this format only
with myView as (Select mp.Id, sum(mp.Quantity) counts from Map mp where
mp.bizDate='22-Jan-2019' group by mp.Id)
Select m.Id, mv.counts, (mv.counts - sum(m.Quantity)) from Map m
LEFT OUTER JOIN myView mv ON
m.id=mv.Id where m.bizDate='25-Jan-2019' group by m.Id, mv.counts
The issue is when Map table has no records it doesn't return any data, even if myView is having records. I need records in any case if one of them has data (either of myView or Map).
Sample Data:
Table: Map
Id Quantity BizDate
A 100 22-Jan-2019
A 300 22-Jan-2019
A 300 25-Jan-2019
B 100 22-Jan-2019
B 200 25-Jan-2019
C 500 22-Jan-2019
D 300 25-Jan-2019
The Expected Output shoud be:
Id Counts (counts-Quantity)
A 400 100 --> (400-300)
B 100 -100 --> (100-200)
C 500 500 --> (500-NA)
D 0 -300 --> (NA-300)