Oracle outer join "entity" - sql

How can you refer to the "combined entity" created from an outer join within your query? Specifically how can you replace the "??" in the query below:
SELECT TableA.x,
??.y --How do you select from the combined entity?
FROM TableA,
TableB buys
FULL OUTER JOIN TableB sells ON buys.run_id = sells.run_id
AND buys.specie_id = sells.specie_id
AND buys.account_id = sells.account_id
WHERE TableA.id = ??
AND -- want to join this with the "combined entity" resulting from the outer join
buys.buy = 'Y'
AND -- Is this valid or does this have to be within the Outer join statement?
sells.buy = 'N';

select TableA.x, fullTable.y
from TableA
INNER JOIN
( SELECT y
FROM TableB buys
full outer join TableB sells
on buys.run_id = sells.run_id
and buys.specie_id = sells.specie_id
and buys.account_id = sells.account_id
AND buys.buy = 'Y' AND sells.buy = 'N'
) AS fullTable
ON TableA.id = fullTable.y
The condition can be in the final WHERE or ON like this but it essentially cancels the full join:
select TableA.x, fullTable.y
from TableA
INNER JOIN
( SELECT y
, buys.buy AS buysBuy --- fields here so they can be
, sells.buy AS sellsBuy --- used in the final WHERE or ON
FROM TableB buys
full outer join TableB sells
on buys.run_id = sells.run_id
and buys.specie_id = sells.specie_id
and buys.account_id = sells.account_id
) AS fullTable
ON TableA.id = fullTable.y
AND buysBuy = 'Y' AND sellsBuy = 'N' --- the condition here

Presumably what you're trying to ask is how to refer to the columns used in the join in other parts of the query. There is no joined entity per se; you still have to refer to the columns from the tables (in this case "buys" or "sells", it just happens that the columns used in the join will have the same value for both tables.
If you want whichever is not null (since this is a full outer join), you can use coalesce or nvl to find find the non-null value:
SELECT TableA.x,
nvl(buys.run_id,sells.run_id) as run_id,
...

You need the sells.buy = 'N' and buys.buy = 'Y' predicates inside the outer join.
SELECT TableA.x,
??.y --How do you select from the combined entity?
FROM TableA,
INNER JOIN TableB buys ON <whatever>
AND buys.buy = 'Y'
FULL OUTER JOIN TableB sells ON buys.run_id = sells.run_id
AND buys.specie_id = sells.specie_id
AND buys.account_id = sells.account_id
AND sells.buy = 'N'
As for ??.y, you need to specify whichever table you want it from - TableA, buy or sell.

Related

How to join two tables on multiple columns using OR condition in bigquery SQL

Lets say I have two tables.
First table shown below:
tableA
Second table
tableB
Now I want to write a query That will join the two tables above on either name OR email OR phone.
Something like:
SELECT * FROM tableA
LEFT JOIN tableB
ON
(tableA.name_A = tableB.name_B OR tableA.email_A = tableB.email_B OR tableA.phone_A = tableB.phone_B)
And it should produce a table something like this
If you notice,
John matches rows between tableA and tableB on name.
Ally/allie matches rows between tableA and tableB on email.
Sam/Samual matches rows between tableA and tableB on phone
When I try to do this same query though I receive an
error that says LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
I am using BigQuery.
Please help, cheers
Try INNER JOIN
SELECT * FROM tableA
INNER JOIN tableB
ON
(tableA.name_A = tableB.name_B OR tableA.email_A = tableB.email_B OR tableA.phone_A = tableB.phone_B)
or CROSS JOIN:
SELECT * FROM tableA
CROSS JOIN tableB
WHERE
tableA.name_A = tableB.name_B
OR tableA.email_A = tableB.email_B
OR tableA.phone_A = tableB.phone_B
or UNION DISTINCT:
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.name_A = tableB.name_B
UNION DISTINCT
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.email_A = tableB.email_B
UNION DISTINCT
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.phone_A = tableB.phone_B
could you try by using parenthesis
SELECT * FROM tableA
LEFT JOIN tableB
ON
(tableA.name_A = tableB.name_B) OR
(tableA.email_A = tableB.email_B) OR
(tableA.phone_A = tableB.phone_B)

MS Acces Jet SQL Error: Join Expression not supported with multiple Join conditions

I'm trying to run this SQL Expression in Access:
Select *
From ((TableA
Left Join TableB
On TableB.FK = TableA.PK)
Left Join TableC
On TableC.FK = TableB.PK)
Left Join (SELECT a,b,c FROM TableD WHERE b > 1) AS TableD
On (TableD.FK = TableC.PK AND TableA.a = TableD.a)
but it keeps getting error: Join-Expression not supported.
Whats the problem?
Sorry, im just starting with Jet-SQL and in T-SQL its all fine.
Thanks
The issue is that the final outer join condition TableA.a = TableD.a will cause the query to contain ambiguous outer joins, since the records to which TableA is joined to TableD will depend upon the results of the joins between TableA->TableB, TableB->TableC and TableC->TableD.
To avoid this, you'll likely need to structure your query with the joins between tables TableA, TableB & TableC existing within a subquery, the result of which is then outer joined to TableD. This unambiguously defines the order in which the joins are evaluated.
For example:
select * from
(
select TableA.a, TableC.PK from
(
TableA left join TableB on TableA.PK = TableB.FK
)
left join TableC on TableB.PK = TableC.FK
) q1
left join
(
select TableD.a, TableD.b, TableD.c, TableD.FK from TableD
where TableD.b > 1
) q2
on q1.a = q2.a and q1.PK = q2.FK
Consider relating every join to the FROM table to avoid having to nest relations.
SELECT *
FROM ((TableA
LEFT JOIN TableB
ON TableB.FK = TableA.PK)
LEFT JOIN TableC
ON TableC.FK = TableA.PK)
LEFT JOIN
(SELECT FK,a,b,c
FROM TableD WHERE b > 1
) AS TableD
ON (TableD.FK = TableA.PK)
AND (TableD.a = TableA.a)

Outer join not showing extra entries

I have made a query in which 3 tables are used. The first table has all the desired names which I need. The 2nd and 3rd table give me those names on which there is some bill amount. But I need all the names from the 1st table as well.
SELECT a.name,
nvl(c.bill_amount,0)
FROM table_1 a left outer join table_2 b
ON a.name = b.name
left outer join table_3 c on B.phone_number = C.phone_number
AND B.email = C.email
where b.status = 'YES'
and a.VALID = 'Y';
Now, the tables b and c give me limited number of names, lets say 5 on which bill is there. But in table_1, there are 10 names. I want to display them also with 0 bill_amount on their name. I'm using Oracle.
Applying a where clause on the right hand tale basiically makes it an inner join. To keep it OUTER, put the condition in the join conditions
Try:
SELECT a.name,
nvl(c.bill_amount,0)
FROM table_1 a
left outer join table_2 b
ON a.name = b.name
and b.status = 'YES' -- Put it here
left outer join table_3 c
on B.phone_number = C.phone_number
AND B.email = C.email
where a.VALID = 'Y'; -- Only items from the left hand table should go in the where clause
The answer above is right , I would just want to be more precise. The fact is when a left join does not match, the column of the right hand table are set to NULL.
Actually NULL is always propagating value in SQL, so b.status = 'YES' have the value NULL if the join does not math, and then the predicate does not match neither.
The general way to handle this would be (b.status = 'YES' or b.name IS NULL) : because b.name is the join column it is null if and only if join does not match, which may not be the case for b.status.
Because NULL is propagating you cannot use field = NULL but field IS NULL instead.
But it is okay to have it in the join clause when it is more clear.

Returning narrowed down SELECT based on associated table

I have a join query I use to pull data from another table:
SELECT [THEME].[NAME],
[THEMETYPE].[TYPE]
FROM [THEME]
LEFT OUTER JOIN [THEMETYPE]
ON [THEME].[THEMETYPEID] = [THEMETYPE].[PK_THEMETYPE]
WHERE COALESCE([THEME].[THEMETYPEID], 'null') LIKE '%'
ORDER BY CASE
WHEN [THEMETYPE].[TYPE] IS NULL THEN 1
ELSE 0
END,
[THEMETYPE].[TYPE]
I need to add the ability to narrow it down if a 3rd tables values match up:
Where producttheme.productid = variable-paramater-here
AND producttheme.themeid = theme.pk_theme
Here is a pic of the table:
So if the 1 is chosen above, it will return all [Theme].[Name] and the associated [ThemeType].[Type] where The ThemeId is associated with ProductId = 1
Edit: to be more clear ThemeId is the Primary key in the Theme table where Theme.Name exists.
This would give you some idea, please adjust the column names accordingly:
SELECT [Theme].[Name], [ThemeType].[Type]
FROM [Theme]
Left Outer Join [ThemeType]
ON [Theme].[ThemeTypeId] = [ThemeType].[PK_ThemeType]
join ProductTheme PT
on PT.ProductID=ThemeType.ProductID
WHERE ProductTheme.ProductID = VARIABLE-PARAMATER-HERE AND ProductTheme.ThemeId = Theme.PK_Theme
ORDER BY [ThemeType].[Type]
Depending on whether or not you need the WHERE condition before you add the 3rd table, you can try one of these 2 options:
SELECT *
FROM TABLE1 T1
LEFT OUTER JOIN TABLE2 T2
ON T1.FIELDA = T2.FIELDA
INNER JOIN TABLE3 T3
ON T1.FIELDA = T3.FIELDA
WHERE T1.FIELDB = 'aaa'
AND T3.FIELDC = 12
or:
SELECT *
FROM (SELECT T1.FIELDA,
T2.FIELDB
FROM TABLE1 T1
LEFT OUTER JOIN TABLE2 T2
ON T1.FIELDA = T2.FIELDA
WHERE T1.FIELDC = 'aaa')T3
INNER JOIN TABLE3 T4
ON T3.FIELDA = T3.FIELDA
AND T4.FIELDC = 12
I hope this gives you something to work with.
If you provide some sample data, I can set up a working example.

How do I get all records from tableA with a left outer join and a where condition on tableB?

Basically, what I want is if there is a record in tableB of type 'X' I want to see it, otherwise I don't, but I want all records from tableA.
I know I could accomplish this by putting the tableB.type = 'X' in the LEFT OUTER JOIN ON clause, but I can't do that because I'm limited to using only the where condition because I'm using a restricted query manager of a program I won't name, but I definitely hate. :)
SELECT *
FROM tableA
LEFT OUTER JOIN ON tableA.ID = tableB.ID
WHERE tableB.type = 'X'
How do I accomplish this?
EDIT
I've tried this, but I still don't get all records from tableA. I'm testing this on SQL server to avoid waiting for long periods for my query to run on the production system. I'm pretty sure the production system is using Oracle if that helps.
SELECT *
FROM tableA LEFT OUTER JOIN ON tableA.ID = tableB.ID
WHERE tableB.type = 'X' OR tableB.type IS NULL
Check for nulls in tableB:
SELECT *
FROM tableA LEFT OUTER JOIN ON tableA.ID = tableB.ID
WHERE tableB.type = 'X'
OR tableB.type IS NULL
That will get you everything from both tables when the join matches, and everything from tableA when there's no corresponding record in tableB.
If type can be null naturally, you'll want to change the condition to something more sound:
SELECT *
FROM tableA LEFT OUTER JOIN ON tableA.ID = tableB.ID
WHERE tableB.type = 'X'
OR tableB.ID IS NULL
Assuming ID is the primary key and cannot be null naturally, that will get the same result.
Don't know if you have access to the database, or if you have to query tableB specifically due to some other restriction, but you could always create a view of table b called tableBTypeX where the view is restricted to only those rows with type = x. Then you could left outer join against tableBTypeX . In your query, the join columns are ID columns, so they probably have indexes, making the query fine in terms of speed. In the case where the join columns are not indexed, joining against the view would be more efficient, because fewer rows are joined against, and joining against unindexed rows usually requires a full table scan, making it a much more time consuming query.
You can join against the "type X" records from tableB exclusively by amending the join condition:
SELECT
*
FROM
tableA
LEFT OUTER JOIN ON
tableA.ID = tableB.ID
AND tableB.type = 'X'
Is UNION possible?
SELECT *
FROM tableA
LEFT OUTER JOIN ON tableA.ID = tableB.ID
WHERE tableB.type = 'X'
UNION
SELECT *
FROM tableA
... or a CTE? Not sure how the name tableB would resolve though and can't test...
;WITH tableB AS
(
SELECT * FROM tableB WHERE type = 'X'
)
SELECT *
FROM
tableA
LEFT OUTER JOIN
tableB ON tableA.ID = tableB.ID