Left Outer Join with a subquery not executing oracle - sql

I am trying to make a query for that contains left outer join and a query that contains a sub query. I tried the following code:
select c.CustomerName,l.Way,l.Travel
from (select l.HomeAdd from tbl_Location l
where l.HomeAdd like '%Colombo%') from
tbl_Customer c left outer join tbl_Location l
on (l.location_id=c.location_id);
I get the following error message
tbl_Customer c left outer join tbl_Location l
*
ERROR at line 4:
ORA-00933: SQL command not properly ended
Please help!!!

Very strange query. I assume you intend:
select c.CustomerName, l.Way, l.Travel
from tbl_Customer c left outer join
tbl_Location l
on l.location_id = c.location_id and l.HomeAdd like 'Colombo%';
The problem with your query is you have subquery in the where clause. There is no comparison -- not =, no exists, no in, nothing that represents a boolean expression.

It looks like you are missing EXISTS keyword in the query
select c.CustomerName,l.Way,l.Travel
from tbl_Customer c left outer join tbl_Location l
on (l.location_id=c.location_id)
where EXISTS (
select l.HomeAdd from tbl_Location l
where l.HomeAdd like 'Colombo%'
);

If you insist upon using a subquery I suggest you do something like:
select c.CustomerName,
l.Way,
l.Travel
from tbl_Customer c
left outer join (SELECT l.HOMEADD,
l.WAY,
l.TRAVEL
FROM tbl_Location
WHERE l.HOMEADD LIKE 'Colombo%') l
on l.location_id=c.location_id
Also, a comment: I strongly suggest that you should not prefix the names of tables etc, with something like 'TBL_'. When tables, etc, are used the context of the usage tells you what the object is. Oracle identifiers are limited to 30 characters - IMO sacrificing 13% of the available name length to a prefix is simply wasteful.
Best of luck.

Related

Can I reference on an alias in Oracle?

I have a query like this:
SELECT
ARUTORZS.ARUKOD,
ARUTORZS.ARUNEV,
(SELECT SUM(MENNYISEG)
FROM ((((STOREIS.KESZLET
LEFT OUTER JOIN STOREIS.THELY ON
THELY.TH=KESZLET.TH)
LEFT OUTER JOIN STOREIS.ZONA ON
ZONA.ZONAKOD=THELY.ZONA)
LEFT OUTER JOIN STOREIS.ARUTELEP ON
ARUTELEP.TH_KOD='P' AND
ARUTELEP.ARUKOD=KESZLET.ARUKOD)
LEFT OUTER JOIN STOREIS.RAKTAR ON
RAKTAR.RAKTAR=KESZLET.RKOD)
WHERE KESZLET.ARUKOD=ARUTORZS.ARUKOD ) AS keszlet,
ARUTELEP.RKOD,
ARUTELEP.ZONA_KOMISSIOS,
ARUTORZS.GYARTO_KOD,
UGYFEL.ORACLE_KOD,
UGYFEL.SZAML_NEV,
ARUTORZS.SULY,
**ARUTORZS.SULY*keszlet/1000 AS SULYKG,**
ARUTORZS.REL_LEJ,
ARUTORZS.GYUJTO,
ARUTORZS.RAKLAP_MENNY
FROM ((STOREIS.ARUTORZS
LEFT OUTER JOIN STOREIS.ARUTELEP ON
ARUTELEP.TH_KOD='P' AND
ARUTELEP.ARUKOD=ARUTORZS.ARUKOD)
LEFT OUTER JOIN STOREIS.UGYFEL ON
UGYFEL.UGYF_KOD=ARUTORZS.GYARTO_KOD)
WHERE ARUTELEP.RKOD>=100 AND ARUTELEP.RKOD<=199
The keszlet subquery works, but Ican't reference on its result in another column. How can I reference on the result of the keszlet subquery? I want to multiply it in another column.
Use a table expression.
For example:
select
x.*,
c * m as r
from (
select
a,
(select max(b) from t) as m,
c
from t
) x
The table expression produces a "named column" m that you can use in the main query in the expression c * m.
You can use your result from your subquery, but u have to use it as a table subquery.
Description
With table subqueries, the outer query treats the result of the
subquery as a table. You can only use these in the FROM clause.
So, your subquery goes after the FROM clause and you can make a new join like this:
SELECT your_result_value * --do what you want...
FROM
(SELECT SUM(MENNYISEG) as YOUR_RESULT_VALUE, PK --your_primary_key
FROM ((((STOREIS.KESZLET
LEFT OUTER JOIN STOREIS.THELY ON
THELY.TH=KESZLET.TH)
LEFT OUTER JOIN STOREIS.ZONA ON
ZONA.ZONAKOD=THELY.ZONA)
LEFT OUTER JOIN STOREIS.ARUTELEP ON
ARUTELEP.TH_KOD='P' AND
ARUTELEP.ARUKOD=KESZLET.ARUKOD)
LEFT OUTER JOIN STOREIS.RAKTAR ON
RAKTAR.RAKTAR=KESZLET.RKOD)
WHERE KESZLET.ARUKOD=ARUTORZS.ARUKOD ) keszlet
JOIN "your_tables" ON keszlet.PK = "another_PK"
--and so on...

use Inner Join in SQL

I want to join two tables and then I want to join this result with another table
but it doesn't work
select * from
(
(select SeId,FLName,Company from Sellers) s
inner join
(select SeId,BIId from BuyInvoices) b
on s.SeId=b.SeId
) Y
inner join
(select * from BuyPayments) X
on Y.BIId=X.BIId
thanks
In most databases, your syntax isn't going to work. Although parentheses are allowed in the FROM clause, they don't get their own table aliases.
You can simplify the JOIN. This is a simpler way to write the logic:
select s.SeId, s.FLName, s.Company, bp.*
from Sellers s inner join
BuyInvoices b
on s.SeId = b.SeId inner join
BuyPayments bp
on bp.BIId = b.BIId;

I want to use a declared table name in inner select statement but cant

SELECT DISTINCT
W.AVG_WGHT
FROM
***[LOT] L***
INNER JOIN [DBO].[OWNR_MKUP] MK ON L.[OWNR_INFO_ID= MK.[OWNR_ID]
,(SELECT [AVG_WGHT]
FROM [DBO].[DLY_LOT_SUM] DLS
INNER JOIN [DBO].[LOT] L ON DLS.[LOT_ID] = L.[LOT_ID]
WHERE DLS.[COST_DTE] = (SELECT MAX(DTE) FROM [DBO].[DLY_HEAD_CNT] WHERE [LOT_ID] = L.[LOT_ID] AND [CUR_HEAD_CNT] > 0 AND [DTE] <= #COST_DTE
) W
In the inner SELECT statement I have used inner join on LOT L again but I don't want to do that I want to use the LOT L which was previously declared but SQL doesn't allow it to use.
Because of the inner join on LOT L once again I am getting multiple records and I don't want to use this inner SELECT statement in upper SELECT DISTINCT
You are using both explicit join syntax and implicit (comma separated) join syntax. When you implicitly join your subquery without conditions, you get a cartesian product. Comma separated joins have been replaced by explicit join syntax in ANSI-SQL in 92, the use of comma separated joins is discouraged.
What you probably need is:
SELECT DISTINCT
DLS.AVG_WGHT
FROM [LOT] L
INNER JOIN [DBO].[OWNR_MKUP] MK
ON L.[OWNR_INFO_ID= MK.[OWNR_ID]
INNER JOIN [DBO].[DLY_LOT_SUM] DLS
ON DLS.[LOT_ID] = L.[LOT_ID]
--WHERE DLS.[COST_DTE] = [DBO].[DLY_HEAD_CNT] I don't understand this line, what is `[DBO]`?

SQL syntax error Multiple inner join

SELECT *
FROM tproducts
INNER JOIN torder ON tproducts.Product_ID=torder.Product_ID
INNER JOIN tcustomer ON torder.Customer_ID=tcustomer.Customer_ID
Can anyone see what is wrong with this as VB.net says that there is a missing operator and i cant spot it?
In MS Access, you need to use parentheses for multiple joins:
SELECT *
FROM (tproducts INNER JOIN
torder
ON tproducts.Product_ID = torder.Product_ID
) INNER JOIN
tcustomer
ON torder.Customer_ID = tcustomer.Customer_ID;
No other database requires this, and using parentheses like this looks awkward for any other database.

SQL joining three tables, join precedence

I have three tables: R, S and P.
Table R Joins with S through a foreign key; there should be at least one record in S, so I can JOIN:
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
If there's no record in S then I get no rows, that's fine.
Then table S joins with P, where records is P may or may not be present and joined with S.
So I do
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
LEFT JOIN P ON (P.id = S.fkp)
What if I wanted the second JOIN to be tied to S not to R, like if I could use parentheses:
SELECT
*
FROM
R
JOIN (S ON (S.id = R.fks) JOIN P ON (P.id = S.fkp))
Or is that already a natural behaviour of the cartesian product between R, S and P?
All kinds of outer and normal joins are in the same precedence class and operators take effect left-to-right at a given nesting level of the query. You can put the join expression on the right side in parentheses to cause it to take effect first. Remember that you will have to move the ON clauses around so that they stay with their joins—the join in parentheses takes its ON clause with it into the parentheses, so it now comes textually before the other ON clause which will be after the parentheses in the outer join statement.
(PostgreSQL example)
In
SELECT * FROM a LEFT JOIN b ON (a.id = b.id) JOIN c ON (b.ref = c.id);
the a-b join takes effect first, but we can force the b-c join to take effect first by putting it in parentheses, which looks like:
SELECT * FROM a LEFT JOIN (b JOIN c ON (b.ref = c.id)) ON (a.id = b.id);
Often you can express the same thing without extra parentheses by moving the joins around and changing the direction of the outer joins, e.g.
SELECT * FROM b JOIN c ON (b.ref = c.id) RIGHT JOIN a ON (a.id = b.id);
When you join the third table, your first query
SELECT
*
FROM
R
JOIN S ON (S.id = R.fks)
is like a derived table to which you're joining the third table. So if R JOIN S produces no rows, then joining P will never yield any rows (because you're trying to join to an empty table).
So, if you're looking for precedence rules then in this case it's just set by using LEFT JOIN as opposed to JOIN.
However, I may be misunderstanding your question, because if I were writing the query, I would swap S and R around. eg.
SELECT
*
FROM
S
JOIN R ON (S.id = R.fks)
The second join is tied to S as you explicity state JOIN P ON (P.id = S.fkp) - no column from R is referenced in the join.
with a as (select 1 as test union select 2)
select * from a left join
a as b on a.test=b.test and b.test=1 inner join
a as c on b.test=c.test
go
with a as (select 1 as test union select 2)
select * from a inner join
a as b on a.test=b.test right join
a as c on b.test=c.test and b.test=1
Ideally, we would hope that the above two queries are the same. However, they are not - so anybody that says a right join can be replaced with a left join in all cases is wrong. Only by using the right join can we get the required result.