0

I have two views, which contains an ID, Unit and Quantity. One view is for actuals and one for estimates. I want a select statement that will return all rows from both views, for a specific ID, regardless of whether the units are in both views. I can't seem to get this to work with the FULL OUTER JOIN, as I need to join on the ID and the Unit.

enter image description here

In this example vVMVoyEstTotalBLQuantitySrch contains an entry for CBM, which is not in vVMVoyBLQuantitySrch. I still want all the lines, eg:

enter image description here

It could also be the other way around, eg that vVMVoyEstTotalBLQuantitySrch does not contain a specific unit, but vVMVoyBLQuantitySrch does, and again I want all rows. If it is in at least one of the views, return it.

I have tried some code snippets from the internet, using COALESCE on both ID and Unit, but then I only get the missing entry (CBM), and not all.

Please help!

Community
  • 1
  • 1

2 Answers2

0

Does your attempt look like this?

select . . .
from view1 v1 full outer join
     view2 v2
     on v1.id = v2.id
where 15506 in (v1.id, v2.id);

The trick is that you have to check both ids, because the full outer join is likely to produce NULL values.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

try Below query

SELECT ISNULL(q1.Id,q2.Id) As Id, 
ISNULL(q1.estLoadUnit,q2.ActLoadUnit) As Units,
q1.EstTotalLoaded,
 q2.ActTotalLoadded
FROM vVMVoyEstTotalBLQuantitySrch  q1
FULL OUTER JOIN vVMVoyBLQuantitySrch q2 ON q1.Id = q2.Id
                     AND q2.actLoadUnit = q1.EstLoadUnit
Rajat Jaiswal
  • 645
  • 4
  • 15