Explicit join Using multiple tables - explicit

I need to join 3 tables using an explicit join. There is not a common key across all three tables.

If you need to join tables you need a common key for every pair.
So you can try something like this:
SELECT t1.*, t2.*, t3.*
FROM table1 t1 INNER JOIN table2 t2
ON t1.id1 = t2.id1
INNER JOIN table3 t3
ON t2.id2 = t3.id2

Related

Inner join in U-SQL with multiple OR condition

I am trying to perform a left join between two tables ( see the below code):
Select e.EmpID, EmpName
FROM #Emp AS e
LEFT JOIN #Dept AS d
ON e.EmpID==d.EmpID1 OR e.EmpID==d.EmpID2
I am getting below error:
How to fix this, I need to add multiple Join condition with OR keyword

Mulitple tables join in Hive getting error - Both left and right aliases encountered in join

I am trying to join 3 tables. Following are the table details.
I am expecting following results
Here is my query and getting error as "both left and right aliases encountered in join 'id'".
This was due to joining 3rd table with 1st and 2nd table(last full join statement).
select coalesce(a.id,b.id,c.id) as id,
ref1,ref2,ref3
from v_cmo_test1 a
FULL JOIN v_cmo_test2 b on (a.id = b.id)
FULL JOIN v_cmo_test3 c on (c.id in (a.id,b.id))
If I am using below query, id 3 is repeating in the table which I don't want.
select coalesce(a.id,b.id,c.id) as id,
ref1,ref2,ref3
from v_cmo_test1 a
FULL JOIN v_cmo_test2 b on a.id = b.id
FULL JOIN v_cmo_test3 c on c.id = a.id
Could any one help me on how to achieve the expected results and really appreciate for your help.
Thanks, Babu
This is a very tricky requirement. data is incorrect because you are using test1 as driver, outer joins arent working properly. And this can occur with other tables. So, i am joining two tables at a time to achieve what you want.
select coalesce(inner_sq.id,c.id) as id,ref1,ref2,ref3
from
(select coalesce(a.id,b.id,c.id) as id,ref1,ref2
from v_cmo_test1 a
FULL JOIN v_cmo_test2 b on a.id = b.id
) inner_sq
FULL JOIN v_cmo_test3 c on c.id = inner_sq.id
Inner_sq query output -
1,bab,kim
2,xxx,yyy
3,,mmm
When you full join above with test3, you should get your output.

How can we construct a query containing Union with JoinSqlBuilder in SeviceStack OrmLite

I have a query like this where I need to union two tables and then join it with a third one
SELECT *
FROM (SELECT * FROM Table1 where col2_id = 1
UNION ALL
SELECT * FROM Table2 where col2_id = 1) AS UnionList
INNER JOIN Table3 t3 ON t3.id = UnionList.col3_id
I am trying to use JoinSqlBuilder in ServiceStack OrmLite to build this query. I am able to create the join part but I am confused about how to create the Union part. Did not find any option/function which union's two table.

SQL Get specific columns from one table and all rows from a joined table in one query

This may have been asked before and I just can't find it.
I have a one to many relationship in the database on a few tables.
table1
table2
table3
table2 - table3 is the 1-many relationship
here's a mock of what I have:
select
table1.id
table1.Column
table2.Column2
-- I want all entries here from table 3 here as well
From table1 t1
left outer join table2 t2 on t2.ID = t1.ID
left outer join join table3 t3 on t3.ID2 = t2.ID2
Is it possible to also select all of the entries that belong to table3 in this query without specifying a sub-query in the select statement?
Also, does this look right? As I've said in the past I'm really new to SQL, thus my sucky code...
EDIT
Sorry guys I misspoke. I need a single column from each of the rows that should be in table3
select
table1.id,
table1.Column,
table2.Column2,
-- I'm going to need a subquery here aren't I...?
table3.columnFromRrow1,
table3.columnFromRrow2,
table3.columnFromRrow3
From table1 t1
left outer join table2 t2 on t2.ID = t1.ID
left outer join join table3 t3 on t3.ID2 = t2.ID2
;WITH cte AS
( SELECT table1.t1id,
table1.t1col,
table2.t2col,
table3.t3col,
ROW_NUMBER() OVER (PARTITION BY t1id,t1col,t2col
ORDER BY table3.id) AS RN
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t2.ID = t1.ID
LEFT OUTER JOIN
JOIN table3 t3
ON t3.ID2 = t2.ID2
)
SELECT
t1id,
t1col,
t2col,
MAX(CASE WHEN RN=1 THEN t3col END) AS columnFromRrow1,
MAX(CASE WHEN RN=2 THEN t3col END) AS columnFromRrow2,
MAX(CASE WHEN RN=3 THEN t3col END) AS columnFromRrow3
FROM cte
WHERE RN<=3
GROUP BY t1id,t1col,t2col
I've modified (and corrected your query to do what you want).
SELECT
table1.id,
table1.Column,
table2.Column2,
table3.* -- All columns from table3
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t2.ID = t1.ID
LEFT OUTER JOIN table3 AS t3
ON t3.ID2 = t2.ID2
NOTE: This answer is no longer valid, because the original question has been modified...
Using *
select
table1.id
table1.Column
table2.Column2
-- I want all entries here from table 3 here as well
table3.*
From table1 t1
left outer join table2 t2 on t2.ID = t1.ID
left outer join join table3 t3 on t3.ID2 = t2.ID2

SQL query for SQL Server Compact Edition 3.5 - GROUP BY issue

SELECT BabyInformation.* , t1.*
FROM BabyInformation
LEFT JOIN
(SELECT * FROM BabyData
GROUP BY BabyID
ORDER By Date DESC ) AS t1 ON BabyInformation.BabyID=t1.BabyID
This is my query. I want to get the one most recent BabyData tuple based on date.
The BabyInformation should left join with babyData but one row per baby...
I tried TOP(1) but this worked only for the first baby
Here is one way to do it, there are other ways which can be faster, but I believe this one to be the clearest for a beginner.
SELECT BabyInformation.*, BabyData.*
FROM BabyInformation
JOIN
(SELECT BabyID, Max(Date) as maxDate FROM BabyData
GROUP BY BabyID
) AS t1
ON BabyInformation.BabyID=t1.BabyID
Join BabyData ON BabyData.BabyID = t1.BabyID and BabyData.Date = t1.maxDate
This should do it:
SELECT bi.* , bd.*
FROM BabyInformation [bi]
LEFT JOIN BabyData [bd]
on bd.BabyDataId = (select top 1 sub.BabyDataId from BabyData [sub] where sub.BabyId = bi.BabyId order by sub.Date desc)
I've assumed that there is a column called 'BabyDataId' in the BabyData table.

Resources