FetchXML with outer joins and nested inner joins: Is it possible to do an outer join on a nested inner join? - inner-join

I am attempting to do some nested joins using FetchXML. I want to do an inner join on two linked child entities, and then do an outer join on that result to retrieve all rows from the parent along with the data from the two linked child entities if it exists. I only want data from childEntity1 if there are records in childEntity2, but I always want all rows from the parent.
In T-SQL it would be something like
SELECT [fields] FROM ParentEntity OUTER JOIN
(childEntity1 INNER JOIN childEntity2 ON childEntity1.id =
childEntity2.childEntity1id)
ON ParentEntity.id = childEntity1.parentEntityId.
In FetchXML, when I use link-type=outer for everything, I get all rows from the parent entity as expected, but when I change the link-type on the child entities to inner, my result set does not include rows where nothing is returned from the join of the two child entities.
Is it possible to nest an inner join like in the SQL statement above in FetchXML?
Here's a simplified version of my FetchXML. When the link-type on childEntity1/childEntity2 is "outer", I get all my rows from the parentEntity, but when it's "inner", the parentEntity's rows are filtered by the results of the child entities' join.
<fetch version='1.0' mapping='logical' distinct='false'>
<entity name='parentEntity'>
<link-entity link-type='outer' name='childEntity1' from='parentEntityId' to='parentEntityId'>
<link-entity link-type='inner' name='childEntity2' from='childEntity1Id' to='childEntity1Id'>
</link-entity>
</link-entity>
</entity>
</fetch>

See my comment above. SQL Profiler came to my rescue and showed me exactly what was getting sent to the database, and the joins were being nested the way I wanted.

Related

Access 2016 two table join based on a field found in another table

In advance, thank you for your assistance and explanation, I am a novice at best and trying to teach myself.
I have two different types of claims tables, [claim_a] and [claim_b]. Each of these tables capture similar data and have one claim record per row. I have a similar form to display data by record for each table. One of my forms has a grid that captures documents sent and the date sent.
In a third table, [documents], each instance of a document sent is associated with an ssn, claim_num, first, last and clm_type, doc_type and date_sent.
I want to create one query that would output all correspondence sent for both claim tables. I realize I could just do two individual queries but I think this can be done and is not too difficult, I am just missing something and would like to know what. I have tried various join type (inner, left, right) and get various results but nothing that is actually correct. With INNER JOIN, I only got 78 records but am expecting 2,261 and when I did LEFT OUTER, I got 3,070 which totals more than what I had in my [documents] table-I do understand that an outer join with one row in the LEFT table that matches two rows in the RIGHT table will return as two ROWS.
I have also been sure to use parenthesis in my first join statement which based on Google searches seems to be related to Access. I also tried using where clauses too.
I think the problem may be that some of the records in [documents] do not correspond to a record in either claims table. I also just tried joining one claim table to [documents] but even that did not return the expected number of results.
Here are few of the joins I have tried:
Inner Join for one table: My output was missing 4 records for an SSN with 6 total records and I could not figure out why it skipped over the remaining 4. It was only for this SSN. I had other SSNs with more than 6 records.
SELECT documents.date, documents.doc_type,
FROM documents INNER JOIN claim_a ON documents.ssn =
claim_a.ssn WHERE (((documents.clm_type)="Life Only")) OR
(((documents.clm_type)<>("Health")) AND (("Life/ ADB")<>False) AND
(("Life")<>False));
I got 78 records with this join
SELECT documents.date, documents.doc_type,
FROM (documents INNER JOIN claim_a ON documents.ssn =
claim_a.ssn) INNER JOIN claim_b ON documents.ssn =
claim_b.ssn;
I got 3070 records with this join
SELECT documents.date, documents.doc_type,
FROM (documents LEFT OUTER JOIN claim_a ON documents.ssn =
claim_a.ssn) LEFT OUTER JOIN claim_b ON documents.ssn =
claim_b.ssn;
I got the correct number of results with this query but I am concerned it will not work with my Master Form to display header specific information for my form associated with table, claim_b.
SELECT documents.date, documents.doc_type,
FROM documents LEFT JOIN claim_a ON documents.ssn =
claim_a.ssn WHERE (((documents.clm_type)<>""));
I am obviously doing something wrong. Can someone please advise?
Sounds like you need a Union query between the two claims tables to get a list of all claims. Then use the results of theat query to get the document list.
Union query
Select ssn from claim_a
Union all select ssn from claim_b
Save this with a name like SSN_List, then join it to Documents in another query
Select * from Documents
left join SSN_List on Documents.ssn=SSN_List.ssn
And of course change the 2nd query as needed to get the information you need from Documents.
This can probably be done in one query, but I find it easier to understand and use the 2 step approach.

How to create a readable view object based on two tables having both inner and outer joins

I have Table1 having a1,a2,a3,a4 fields and Table2 having b1,b2,b3,b4 fileds.
Join:
inner join Table1.a1=Table2.b2 AND outer join Table1.a3=Table2.b3
How could I create Entity based VO having these both inner and outer joins?
You can try modifying the generated query by hand (custom query), after you create the VO based on both EO's in the traditional way.

JOIN vs Multiple FROM Tables

Is there ever a case where a join will not return data that a FROM multiple tables with the same conditions returns?
e.g.
SELECT *
FROM TableNames as Names
INNER JOIN TableNumbers as Numbers on Names.ID = Numbers.ID
VS
SELECT *
FROM TableNames as Names, TableNumbers as Numbers
WHERE Names.ID = Numbers.ID
An INNER JOIN (as in your first example) will always return the same data as your a cartesian join with a WHERE filter that uses the same join criteria (your second example).
However, note that this is not true for OUTER JOINs, where NULL values are filtered out in a cartesian join with a WHERE filter as join criteria.
Simply, both the queries are same and do the same thing.
Inner Join is generally considered more readable, especially when you join lots of tables.
The WHERE syntax is more relational model oriented.

update and join tables without a where

I'm trying to get the relationships of two databases straighten out and I have an update clause like this
update dbo1.table1
set Relationship_column = table2.id
from dbo1.table2 as table2
inner join dbo2.table3 as Table3
on table2.number = Table3.number
Basically if I join two tables and update a 3rd table based on the results. This works on SSIS because you can just remap the entire table, I dont want to do that.
Of course this does not work in SQL because I need a WHERE clause else each record is repeated on all columns.Is there a work around to this??

SQL Server: Performant way of getting data from two tables

I have to search in two tables that are in a parent-child relation (1:n) but in the result set I only need fields of the parent table. What is the most meaningfull way to do this? Is it one of the following simplified constructions ...
SELECT parent.fields FROM parent LEFT OUTER JOIN child ON [JoinExpr]
Where [ParentFilter] OR [ChildFilter] GROUP BY parent.fields
SELECT DISTINCT(parent.fields) FROM parent LEFT OUTER JOIN child ON [JoinExpr]
Where [ParentFilter] OR [ChildFilter]
SELECT parent.fields FROM parent Where [ParentFilter]
OR parent_id IN(SELECT parent_id FROM child WHERE [ChildFilter])
... or are there other, better possibilities? The data will be queried by ado.net.
select parent.fields
from parent
where <filters on parent columns>
OR exists(
select 'x'
from child
where child.parent_id = parent.parent_id
and <filters on child columns>);
Why are you insisting on using an outer join. From your description the results are going to come from the parent table and the child table will only be used as part of the restriction. In this case you should use an inner join. If the child table does not have a match on a specific criteria then it won't show in your results. The performance will really come down to the fields that make up the join and the filter criteria. Try to make sure that the fields in the join (foreign key) are indexed and where necessary the filter criteria fields are also indexed. Don't use functions in the filter criteria e.g. where field1 >= DATEDIFF(???). I forget the exact syntax for datediff. This will mean that datediff is evaluated for each row in the full set of data. In cases like this a sub-query can be useful

Resources