Entity framework join table against a query - sql

I am using Entity Framework 6.1.1 and I have a query that joins a table against another select statement:
SELECT *
FROM Table1 T1
LEFT OUTER JOIN (SELECT * FROM Table2 WHERE Field = 123) AS T2 ON T1.Field = T2.Field
Is it possible to write something like this using Entity Framework's query syntax?

from t1 in Table1
join t2 in Table2.Where(t => t.Field == 123) on t1.Field equals t2.Field into t2j
select new {
T1 = t1,
T2 = t2j.DefaultIfEmpty(),
}
You can filter Table2 before the join and then use the group join + DefaultIfEmpty from https://msdn.microsoft.com/en-us/library/bb397895.aspx:
You can use LINQ to perform a left outer join by calling the DefaultIfEmpty method on the results of a group join.

Related

SQL JOIN USING and WHERE

This is somewhat of a followon to SQL JOIN where to place the WHERE condition?
I would prefer to use the USING clause on the join, but am not yet able to put the field value condition with the join.
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id) AND t2.field = 0;
ORA-00933: SQL command not properly ended
Is it possible to have USING and another condition as part of the JOIN clause?
You can use:
SELECT 1
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0;
Using USING(id) is like using ON t1.id = t2.id except that in the JOIN result instead of two columns t1.id & t2.id there is only one id column.
For INNER JOIN USING with a condition followed by an OUTER JOIN you need a subquery to keep the WHERE with the USING:
SELECT ...
FROM (SELECT id, ...
FROM table1 t1
JOIN table2 t2 USING(id)
WHERE t2.field = 0) s
LEFT JOIN ...;
For an OUTER JOIN USING with a condition you need a subselect:
SELECT ...
FROM table1 t1
LEFT JOIN (SELECT *
FROM table2 t2
WHERE t2.field = 0) t2
USING (id);
See this re ON/WHERE with JOIN. See this re ON/WHERE when mixing INNER & OUTER JOINs.

HQL query with Left Join in NHibernate 2

How can I give conditions for left join in NHibernate 2.0 HQL query.
Eg in SQL.
select t1.* from table1 t1
left join table t2 on t2.id = t1.id and t2.column2 = t1.column2
I tried the below HQL query, but got an exception "unexpected token: with"
select t1 from Table1 t1
left join t1.Table2 t2 with t2.column 2 = t1.column2
There is no need to use ON statement on (LEFT or any other) JOIN. That condition will be injected by mapping. There is the HQL doc:
14.3. Associations and joins
So, this HQL will work:
SELECT t1 from Entity1 AS t1
LEFT JOIN t1.ReferencePropertyToEntity2 t2
and the generated sql will be like this:
SELECT t1 from Table1 AS t1
LEFT JOIN Table2 t2
ON t1.ReferenceColumnToTable2 = t2.Table2_ID
But in case, that we want to do more restrictions, we can extend ON clause with more conditions - but they will be all applied with AND
SELECT t1 from Entity1 AS t1
LEFT JOIN t1.ReferencePropertyToEntity2 t2
WITH t2.IsActive = 1
OR t1.IsDeleted = 0
will result in
SELECT t1 from Table1 AS t1
LEFT JOIN Table2 t2
ON t1.ReferenceColumnToTable2 = t2.Table2_ID
AND (
t2.IsActive = 1 OR t1.IsDeleted = 0
)
So, in case that we want to use WITH to totally replace ON generated by mapping, we have to go different way - with CROSS JOIN:
NHibernate HQL Inner Join (SQL Server,Visual C#)
How to join Two tables of two non relashinship defined columns using Nhibernate QueryOver

SQL join order and conditions

I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...

SQL query - filter with cascading conditions

I've two tables T1 and T2 with the following columns -
T1
Project_ID
Category
Column_X
Column_Y
Column_Z
T2
Proj_ID
Category
Parent_Project_ID
I want to write a query to get records from T1 with the following condition -
Get Projects with Category = "A" from T1
Get child projects of the above filtered projects
I'm not sure how to check the second condition only with the results coming out of first condition.
What is needed?
Projects from T1 where Category is A
Child projects of projects obtained from condition 1
Adding sample data and desired results as requested -
To get all records from second table then you can use the following query.
SELECT
t2.*
FROM T1 t1
RIGHT OUTER JOIN T2 t2 ON t1.Project_ID = t2.Project_ID
WHERE t1.Category = "A"
SELECT * FROM T2 WHERE T2.Proj_ID IN ( SELECT Project_ID FROM T1 WHERE Category = 'A' )
This should do the job needed.
SELECT * from T2 as d
WHERE EXISTS ( SELECT * from T1 as d1 where d1.Category = 'A' and d1.Project_ID = d.Proj_ID )
SELECT * from T1 as d1 right join T2 as d2 on d1.Project_ID = d2.Proj_ID
WHERE d1.CodTert = 500
I've made an update, these query give the same result, one uses the JOIN one doesn't.
I'm assuming that T2.Parent_Project_ID and T1.Project_ID are related. If so, you can use this:
Select T3.*
From T1
Join T2 On T2.Parent_Project_ID = T1.Project_ID
Join T1 T3 On T3.Project_ID = T2.Proj_ID
Where T1.Category = 'A'
This would get only child projects of projects that have a category of 'A'.
EDIT:
Based on the output format that has been added to the question, the following query, which uses a LEFT OUTER JOIN would render the exact result required:
SELECT
T2.PROJ_ID Project_ID,
T2.Category,
T1.Column_X,
T1.Column_Y,
T1.Column_Z,
T2.Parent_Project_ID
FROM T1 T1_PARENTS
INNER JOIN T2
ON T2.Parent_Project_ID = T1.Project_ID and T1.Category = 'A'
INNER JOIN T2 T2_CHILDREN
ON T2_CHILDREN.PROJ_ID = T2.Parent_Project_ID OR T2_CHILDREN.Parent_Project_ID = T2.Parent_Project_ID
LEFT OUTER JOIN T1
ON T2_CHILDREN.PROJ_ID = T1.Project_ID;

SQL: Select and Inner Join three tables in one query

I am trying to JOIN the result of the first SELECT clause, between the first 2 tables (t1 and t2), with the third table (t3):
SELECT t1.*, t2.PropertyCode, t3.TBMonth
FROM Test.dbo.DailyBudgetExtract T1 , Test.dbo.DailyPropertylListExtract T2
WHERE t1.propertyid = t2.proplistid
OR t1.propertyid = t2.propertyid
INNER JOIN Test.dbo.DailyTrialBalanceExtract T3 ON t1.AccCode = t3.AcctCode
What am I doing wrong?
Here is the correct syntax for your query:
SELECT t1.*, t2.PropertyCode, t3.TBMonth
FROM Test.dbo.DailyBudgetExtract T1 JOIN
Test.dbo.DailyPropertylListExtract T2
ON t1.propertyid = t2.proplistid OR t1.propertyid = t2.propertyid INNER JOIN
Test.dbo.DailyTrialBalanceExtract T3
ON t1.AccCode = t3.AcctCode;
The where clause goes after the from clause. But you don't need a where clause, just put the condition in the on clause, where it should go for an explicit join.
This syntax might be clearer:
SELECT
t1.*,
t2.PropertyCode,
t3.TBMonth
FROM
Test.dbo.DailyBudgetExtract T1
JOIN
Test.dbo.DailyPropertylListExtract T2
ON
( t1.propertyid = t2.proplistid
OR
t1.propertyid = t2.propertyid
)
INNER JOIN
Test.dbo.DailyTrialBalanceExtract T3
ON
t1.AccCode = t3.AcctCode
;
Join are part of the projection, not selection (SQL databases make use of some relational algebra). Even if it happens to be executable in some DB (which I donĀ“t think it is) you should avoid it.
You should use EXISTS and NOT EXISTS (which are knows as semi joins) instead.
The query below is a "transliteration" of your query. If it does not return what you want let me know.
SELECT t1.*,
t2.PropertyCode,
t3.TBMonth
FROM Test.dbo.DailyBudgetExtract T1
JOIN Test.dbo.DailyPropertylListExtract T2
ON (t1.propertyid = t2.proplistid OR t1.propertyid = t2.propertyid)
JOIN Test.dbo.DailyTrialBalanceExtract T3
ON t1.AccCode = t3.AcctCode
If you happen to not need the "t3.TBMonth" field
SELECT t1.*,
t2.PropertyCode
FROM Test.dbo.DailyBudgetExtract T1
JOIN Test.dbo.DailyPropertylListExtract T2
ON (t1.propertyid = t2.proplistid OR t1.propertyid = t2.propertyid)
WHERE EXISTS(SELECT 1 FROM Test.dbo.DailyTrialBalanceExtract T3 WHERE t1.AccCode = t3.AcctCode)
TIPS and NOTES:
At least in SQL Server there is no diff between "INNER JOIN" and "JOIN", so pick one and stay with it;
Using "... FROM table1 t1, table t2" is the same as CROSS JOIN;
Avoid having to do JOIN conditions with OR (you do a JOIN [aka INNER JOIN] with your "... FROM table1 t1, table t2" plus "t1.propertyid = t2.proplistid OR t1.propertyid = t2.propertyid") they slow down the query a lot;