use Inner Join in SQL - 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;

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...

SQL query with 2 counts and 2 left outer joins

Im trying to show all columns from my t1_elem table and join 2 columns in which I use COUNT.
I used query:
SELECT p.*,COUNT(t4_id) as ile_publikacji, COUNT(t7_id) as ile_fitow
FROM t1_elem p
LEFT OUTER JOIN t4_autorzy ON p.t1_id=t4_autorzy.t4_t1_id
LEFT JOIN t7_pliki ON p.t1_id=t7_pliki.t7_t1_id
GROUP BY t1_id
But the results are bad. What I'm doing wrong?
Probably you have multiple matches. As stated, the two counts will be the same. The simplest solution is probably to use distinct:
SELECT p.*, COUNT(DISTINCT t4_id) as ile_publikacji, COUNT(DISTINCT t7_id) as ile_fitow
FROM t1_elem p LEFT JOIn
t4_autorzy
ON p.t1_id = t4_autorzy.t4_t1_id LEFT JOIN
t7_pliki
ON p.t1_id=t7_pliki.t7_t1_id
GROUP BY t1_id

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]`?

Transact SQL JOIN

I have the following query:
SELECT MS.idReg, MS.dsMotivo, A.contrato FROM MS
INNER JOIN S
ON S.motivoSiniestro = MS.idReg
INNER JOIN C
ON C.n__contrat = S.n__contrat
INNER JOIN A
ON A.n__article = C.n__article
Table MS has only 12 records, the ones I need and others have many more entries.
My problem is that I only want the 12 records from MS and their contrato column but I'm getting much more that that. Have tried many combinations of INNER, OUTER, LEFT and RIGHT joins. Any help?
You get too many records because there are several A.contrato values for each row in the MS table. Sql server does not know which one of all the A.contrato values to take so it returns all of them. First you need to decide which one you want.
If any will do you can simply write your query like this:
SELECT MS.idReg, MS.dsMotivo, MAX(A.contrato)
FROM MS
INNER JOIN S
ON S.motivoSiniestro = MS.idReg
INNER JOIN C
ON C.n__contrat = S.n__contrat
INNER JOIN A
ON A.n__article = C.n__article
GROUP BY MS.idReg, MS.dsMotivo
Try this one -
SELECT MS.idReg, MS.dsMotivo, A.contrato
FROM dbo.MS
OUTER APPLY (
SELECT TOP 1 A.contrato
FROM dbo.S
JOIN dbo.C ON C.n__contrat = S.n__contrat
JOIN dbo.A ON A.n__article = C.n__article
WHERE S.motivoSiniestro = MS.idReg
) s

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.