Linq query for joining three tables - asp.net-mvc

I have three tables table1 (main table), table2, table3.
table1 contains table1Id
table2 and table3 contain table2Id, table2RoleId, table3Id, table3RoleId.
Also the same value of table1Id, more than one record in table2Id and table3Id but the table2RoleId's and table3RoleId's are different.
I want to join table1 with table2 and table3 to display like
Table2RoleId and Table3RoleId has to display according to the Table1Id
How can I achieve this?
Thanks

i'm ignore the content of your question and will show you sample left join in linq
var result = from x in table1 join y in table2
on x.tableId1 equals y.tableId1
join z in table3 on x.tableId1 equals z.tableId1
Select new {// your return fields}

Related

Join two datasets based on a flag and id

I am trying to join two datasets based on a flag and id.
i.e
proc sql;
create table demo as
select a.*,b.b1,b.2
from table1 a
left join table2 on
(a.flag=b.flag and a.id=b.id) or (a.flag ne b.flag and a.id=b.id)
end;
This code runs into a loop and never produces a output.
I want to make sure that where there are flag values matching get the attributes; if not get the attributes at id level so that we do not have blank values.
This join condition cannot be optimized. It is not a good practice to use or in a join. If you check your log, you'll see this:
NOTE: The execution of this query involves performing one or more Cartesian product joins
that can not be optimized.
Instead, transform your query to do a union:
proc sql;
create table demo as
select a.*,
b.b1,
b.b2
from table1 as a
left join
table2 as b
on a.flag=b.flag and a.id=b.id
UNION
select a.*,
b.b1,
b.b2
from table1 as a
left join
table2 as b
on a.flag ne b.flag and a.id=b.id
;
quit;

Hive-How to join tables with OR clause in ON statement

I've got the following problem. In my oracle db I have query as follows:
select * from table1 t1
inner join table2 t2 on
(t1.id_1= t2.id_1 or t1.id_2 = t2.id_2)
and it works perfectly.
Nowadays I need to re-write query on hive. I've seen that OR clause doesn't work in JOINS in hive (error warning : 'OR not supported in JOIN').
Is there any workaround for this except splitting query between two separate and union them?
Another way is to union two joins, e.g.,
select * from table1 t1
inner join table2 t2 on
(t1.id_1= t2.id_1)
union all
select * from table1 t1
inner join table2 t2 on
(t1.id_2 = t2.id_2)
Hive does not support non-equi joins. Common approach is to move join ON condition to the WHERE clause. In the worst case it will be the CROSS JOIN + WHERE filter, like this:
select *
from table1 t1
cross join table2 t2
where (t1.id_1= t2.id_1 or t1.id_2 = t2.id_2)
It may work slow because of rows multiplication by CROSS JOIN.
You can try to do two LEFT joins instead of CROSS and filter out cases when both conditions are false (like INNER JOIN in your query). This may perform faster than cross join because will not multiply all the rows. Also columns selected from second table can be calculated using NVL() or coalesce().
select t1.*,
nvl(t2.col1, t3.col1) as t2_col1, --take from t2, if NULL, take from t3
... calculate all other columns from second table in the same way
from table1 t1
left join table2 t2 on t1.id_1= t2.id_1
left join table2 t3 on t1.id_2 = t3.id_2
where (t1.id_1= t2.id_1 OR t1.id_2 = t3.id_2) --Only joined records allowed likke in your INNER join
As you asked, no UNION is necessary.

Left outer join with 3 tables and subquery

sorry for the late response.
For a key in table A, there may be 2 or more records present in tables B and C. That is, one another column in these tables will have a date value which would be making the keys unique. So I want to extract the record that has maximum date value. And that's why I am using the max function. I know that the subquery which I have coded should not be included in the ON clause and it would do the filtering before the join statement. So eventually I want to know how to mention the max clause in the query.
Example:
Table A
Key - AAAAA
Table B:
Record 1
Key - AAAAA
Date - 2017-10-01
Record 2
Key - AAAAA
Date - 2017-10-05
I want the only the record AAAAA/2017-10-05 to be selected from the table B
Basically records from table A where A.c3 = 'Y' should be extracted first (assume it gives 500 records)
Then join these 500 records with tables B and C (left outer, to have all the matching records and the non-matching records should have nulls in the columns from the tables B and C)
In tables B and C, if more than 1 record present with different dates, the maximum date field should be extracted.
Hence final output should contain 500 records.
This is all you need for what you describe
SELECT A.A1, A.A2, B.B1, B.B2, C.C1, C.C2
FROM TABLE1 A
LEFT OUTER JOIN TABLE2 B
ON A.A1 = B.B1
LEFT OUTER JOIN TABLE3 C
ON A.A1 = C.C1
WHERE A.C3 = ‘Y’
These lines are causing your problem...basically forcing your outer joins to an inner joins.
AND B.C3 = (SELECT MAX(B3) FROM TABLE2 T1
WHERE T1.B1 = B.B1)
AND C.C3 = (SELECT MAX(C3) FROM TABLE3 T1
WHERE T1.C1 = C.C1)
If there's no match in B or C , then B.C3 and/or C.C3 will be NULL and NULL can't be = to anything (or <> to anything for that matter)
What are you trying to accomplish with the above that you've not included in the question?
Just do it?
SELECT A.A1, A.A2, B.B1, B.B2, C.C1, C.C2
FROM TABLE1 A
LEFT OUTER JOIN TABLE2 B
ON A.A1 = B.B1
LEFT OUTER JOIN TABLE3 C
ON A.A1 = C.C1
WHERE A.C3 = 'Y' and (B.B1 is null or C.B1 is null)

Complex Join to Return SuperSet

Evening All,
I have been chipping away at this one for a while and for some reason i just can't seem to get my logic to return the way I expect it to.
I have 3 Data tables as well as 3 business concept linking tables.
Table1
Table2
Table3
Rules:
Table 1 can be linked to Table 2
Table 1 can be directly linked to table 3
Table 1 can be indirectly linked to table 3 via table 2
I have tried a fair few variations however It seems to truncate records.
SELECT
*
FROM
Table1 T1
INNER JOIN Table1_to_Table2_Link L1 on L1.T1_ID = T1.ID
RIGHT JOIN TABLE2 T2 ON L1.T2_ID = T2.ID
INNER JOIN Table2_to_Table3_Link L2 ON L2.T2_ID = T2.ID
Right JOIN Table3 T3 ON L2.T3_ID = T3.ID
INNER JOIN Table1_to_Table3_Link L3 on T1.ID = L3.T1_ID
Its a bit awkward to explain but in summart
I require All the Data from Table 1
And only the Data in Tables 2 and 3 if they are directly/indirectly related to table 1. Tables 2 and 3 don't necessarily have to have a related business concept.
The Return Expected is;
Any assistance would be kindly appreciated
You were right. It was not that simple. However I could get desired output by below query
SELECT
T1.*,
T2.*,
T3.*
FROM
Table1 T1
LEFT JOIN Table1_to_Table2_Link L1 on T1.ID = L1.T1_ID
LEFT JOIN TABLE2 T2 ON T2.ID = L1.T2_ID
LEFT JOIN (
SELECT T1_ID AS ID,T3_ID AS table3Id FROM dbo.Table1_to_Table3_Link
UNION ALL
SELECT T2_ID AS ID,T3_ID AS table3Id FROM dbo.Table2_to_Table3_Link
) S
ON T1.ID = s.ID
OR t2.ID = s.id
LEFT JOIN dbo.Table3 T3 ON S.table3Id = T3.ID
Hope it helps.

Using UPPER in ON clause of JOIN for Postgres 9.2.4

When joining two tables on a varchar column wrapped in a upper statement, the join does not work if there is a trailing space in both of the varchar values.
In the two examples below, VALUE1 and VALUE2 = 'ABC '
-- Doesn't work
SELECT * FROM TABLE1 INNER JOIN TABLE2
ON UPPER(VALUE1) = UPPER(VALUE2)
-- Works
SELECT * FROM TABLE1 INNER JOIN TABLE2
ON UPPER(TRIM(VALUE1)) = UPPER(TRIM(VALUE2))
Has anyone else run into this problem?
Let's see:
CREATE TABLE table1(value1 varchar(10));
CREATE TABLE table2(value2 varchar(10));
INSERT INTO table1 values('ABC ');
INSERT INTO table2 values('ABC ');
SELECT * FROM TABLE1 INNER JOIN TABLE2
ON UPPER(VALUE1) = UPPER(VALUE2)
Result:
value1 | value2
--------+--------
ABC | ABC
It appears to work.
Make sure you don't use the CHAR(N) type instead of VARCHAR(N), since they have different semantics concerning spaces.

Resources