I have the following tables:
TableA (id, tableB_id, tableC_id)
TableB (id, expirationDate)
TableC (id, expirationDate)
I want to retrieve all the results from TableA ordered by tableB.expirationDate and tableC.expirationDate. How can I do this?
select ta.*
from TableA ta
inner join TableB tb on ta.tableB_id = tb.id
inner join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
Update:
If you are not getting all the records, then you'll need to use a left outer join:
select ta.*
from TableA ta
left outer join TableB tb on ta.tableB_id = tb.id
left outer join TableC tc on ta.tableC_id = tc.id
order by tb.expirationDate, tc.expirationDate
If the result set is empty with the other suggestions, are you sure the data in the tables is actually correctly correlated to each other?
Can you post some sample rows for each table?
Have you tried:
SELECT a.* FROM TableA a
INNER JOIN TableB b on b.id = a.tableB_id
INNER JOIN TableC c on c.id = a.tableC_id
ORDER BY b.expirationDate, c.expirationDatetableB_id
Related
Say you have 3 tables (tableA, tableB, tableC), each with an ID column and a Value column. Some of the tables' IDs match but some don't.
If you do:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableA.ID = tableC.ID)
Is this different from:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableB.ID = tableC.ID)
Or:
SELECT Y.ID FROM
(SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)) X
FULL JOIN tableC ON (X.ID = tableC.ID)) Y
??? Someone please explain if there is a difference. Thanks.
[Oracle SQL Developer version 4.02.15.21]
For starters, here are all 3 statements, syntactically cleaned up:
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON a.ID = c.ID
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON b.ID = c.ID
SELECT COALESCE(X.ID,c.ID)
FROM
( SELECT COALESCE(a.ID ,b.ID) ID
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID) X
FULL JOIN tableC c ON X.ID = c.ID
Surprisingly, the syntax of the first statement produces duplicate values, but statements 2 and 3 work as advertised.
Edit: Upon further testing, statements 1 and 2 are prone to duplicates, depending on which tables overlap. Statement 3 seems to be the only solid approach.
SQLFiddle
I have the following query
Select TA.Column1 , COALESCE(TE.Column2,TA.Column2) as Mydata
from TableA TA
INNER JOIN TableB TB ON (TA.Column2 =TB.Column1)
LEFT JOIN TableC TC ON (TB.Column2 = TC.Column1)
LEFT JOIN TableD TD ON (TC.Column1 = TD.Column1)
LEFT JOIN TableE TE ON(TD.Column2 = TE.Column1)
To get the result I am looking for I need to Join the MyData column with another TableX
e.g INNER JOIN TableX TX ON (TableX.Column1 = MyData) and have TableX.COlumn2 in my select query .
My query is how can I join the COALESCE(TE.Column2,TA.Column2) as Mydata fetched value with a table TableX
On the surface, you should be able to do this. (Did you try that already?)
Select TA.Column1 , COALESCE(TE.Column2,TA.Column2) as Mydata, TX.Column2
from TableA TA
INNER JOIN TableB TB ON (TA.Column2 =TB.Column1)
LEFT JOIN TableC TC ON (TB.Column2 = TC.Column1)
LEFT JOIN TableD TD ON (TC.Column1 = TD.Column1)
LEFT JOIN TableE TE ON(TD.Column2 = TE.Column1)
LEFT JOIN TableX TX ON (COALESCE(TE.Column2,TA.Column2)) = TX.Column1
I have been having a hard time googling an answer for this, but....
can someone explain to me the difference between putting the ON condition of a JOIN with the the JOIN itself vs putting the ON at the end of all the other JOINs.
here is an example http://sqlfiddle.com/#!3/e0a0f/3
CREATE TABLE TableA (Email VARCHAR(100), SomeNameA VARCHAR(100))
CREATE TABLE Tableb (Email VARCHAR(100), SomeNameB VARCHAR(100))
CREATE TABLE Tablec (Email VARCHAR(100), SomeNameC VARCHAR(100))
INSERT INTO TableA SELECT 'joe#test.com', 'JoeA'
INSERT INTO TableA SELECT 'jan#test.com', 'JaneA'
INSERT INTO TableA SELECT 'dave#test.com', 'DaveA'
INSERT INTO TableB SELECT 'joe#test.com', 'JoeB'
INSERT INTO TableB SELECT 'dave#test.com', 'DaveB'
INSERT INTO TableC SELECT 'joe#test.com', 'JoeC'
INSERT INTO TableC SELECT 'dave#test.com', 'DaveC'
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.email = b.email
INNER JOIN TableC c
ON c.Email = b.email;
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c
ON c.Email = b.email
ON a.email = b.email;
I don't understand why these two SELECT statements produce different results.
What matters is orders of joins. Treat your expressions as if every join produced temporary "virtual" table.
So when you write
FROM TableA a
LEFT OUTER JOIN TableB b ON a.email = b.email
INNER JOIN TableC c ON c.Email = b.email ;
then order is as follows:
TableA is left joined to TableB producing temporary relation V1
V1 is inner joined to TableC.
Meanhwile when you write:
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c ON c.Email = b.email ON a.email = b.email;
then order is as follows:
TableB is inner joined to TableC producing temporary relation V1.
TableA is left joined to V1.
Thus results are different. It is generally recommended to use parenthesis in such situations to improve readability of the query:
FROM TableA a
LEFT OUTER JOIN
(TableB b INNER JOIN TableC c ON c.Email = b.email)
ON a.email = b.email;
In your second example, the part ON a.email = b.email belongs to the LEFT JOIN.
If written like this, it means the following:
INNER JOIN TableC with TableB and LEFT OUTER JOIN the result with TableA.
The result will be all rows from TableA joined with those rows from TableB that also have an entry in TableC.
The first example means the following:
LEFT OUTER JOIN TableB with TableA and INNER JOIN TableC with the result. This is equivalent to using an INNER JOIN for TableB.
Explanation: When you LEFT OUTER JOIN TableA with TableB you will get all rows from TableA and for matching rows in TableB you will get that data, too. In your result set you will have rows with b.email = NULL and this will now be INNER JOINed with TableC. As long as there is no entry in TableC with email = NULL you will get the results you observed.
I have three tables I wish to inner join by a common column between them.
Say my tables are;
TableA TableB TableC
I wish to join A-B, but then also B-C all by this common field I will call common.
I have joined two tables like this;
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common
How do I add the third one?
select *
from
tableA a
inner join
tableB b
on a.common = b.common
inner join
TableC c
on b.common = c.common
Just do the same thing agin but then for TableC
SELECT *
FROM dbo.tableA A
INNER JOIN dbo.TableB B ON A.common = B.common
INNER JOIN dbo.TableC C ON A.common = C.common
dbo.tableA AS A INNER JOIN dbo.TableB AS B
ON A.common = B.common INNER JOIN TableC C
ON B.common = C.common
try the following code
select * from TableA A
inner join TableB B on A.Column=B.Column
inner join TableC C on A.Column=C.Column
try this:
SELECT * FROM TableA
JOIN TableB ON TableA.primary_key = TableB.foreign_key
JOIN TableB ON TableB.foreign_key = TableC.foreign_key
If I am using the "COALESCE" function in a SELECT statement. What is the proper way to refer to the column value in the JOIN? For example ..
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON <COALESCE VALUE HERE> = TableD.Name
You would just use LEFT JOIN TableD ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name
There's no way to reference an expression from the SELECT clause anywhere within the query that clause refers to.
This is another way to expand out the COALESCE, which may work better in certain distributions of data
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON (TableA.Name1 = TableD.Name)
OR (TableA.Name1 IS NULL AND TableA.Name2 = TableD.Name)
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2)
FROM
TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID
LEFT JOIN TableC ON TableA.ID = TableC.ID
LEFT JOIN TableD ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name
To what other persons said, I'll add
SELECT Base.* FROM
(SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2) Names
FROM TableA
) Base
LEFT JOIN TableB ON Base.ID = TableB.ID
LEFT JOIN TableC ON Base.ID = TableC.ID
LEFT JOIN TableD ON Base.Names = TableD.Name
And as Gabe suggested, with a CTE (I think it's a little overkill a CTE here, and I love using CTEs)
; WITH Base AS (
SELECT
ID,
COALESCE(TableA.Name1, TableA.Name2) Names
FROM TableA
)
SELECT Base.* From Base
LEFT JOIN TableB ON Base.ID = TableB.ID
LEFT JOIN TableC ON Base.ID = TableC.ID
LEFT JOIN TableD ON Base.Names = TableD.Name
The obvious ways will work:
LEFT JOIN
TableD
ON COALESCE(TableA.Name1, TableA.Name2) = TableD.Name
or:
LEFT JOIN
TableD
ON TableA.Name1 = TableD.Name
OR TableA.Name2 = TableD.Name
(EDIT: the second query won't work, like it says in the comments)