Use query with InnerJoin - sql

I'm trying to pass this query to inner join, but it does not know how?
This is the query I want to use InnerJoin with these values ​​where
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM
app_gpa_ticket.ticket,
app_gpa_ticket.historico,
dados_v3.centro,
dados_v3.eqpto,
dados_v3.interface,
dados_v3.tecnologia
WHERE
ticket.id_ticket = historico.id_ticket AND
centro.id_centro = eqpto.id_centro AND
eqpto.id_eqpto = interface.id_eqpto AND
eqpto.id_rede = ticket.id_rede AND
tecnologia.id_tecnologia = interface.id_tecnologia;
Thank you!

I think you are trying to go to the standard (explicit) syntax. What you need to do is take what would be your JOIN operators in the WHERE clause and move them near the table itself. What you need to know is what tables on the left (Before the INNER JOIN operator you are joining to the right (After the INNER JOIN operator)
SELECT
ticket.id_ticket,
ticket.id_rede,
historico.id_historico,
historico.id_ticket,
centro.id_centro,
eqpto.id_eqpto,
eqpto.nome,
centro.sigla_centro,
interface.id_interface,
interface.id_eqpto,
interface.desig,
tecnologia.descricao,
interface.id_tecnologia,
tecnologia.id_tecnologia,
eqpto.id_centro,
eqpto.id_rede
FROM app_gpa_ticket.ticket
INNER JOIN app_gpa_ticket.historico ON ticket.id_ticket = historico.id_ticket
INNER JOIN dados_v3.eqpto ON eqpto.id_rede = ticket.id_rede
INNER JOIN dados_v3.interface ON eqpto.id_eqpto = interface.id_eqpto
INNER JOIN dados_v3.centro ON centro.id_centro = eqpto.id_centro
INNER JOIN dados_v3.tecnologia ON tecnologia.id_tecnologia = interface.id_tecnologia

Related

LEFT JOIN expression not supported

I need to do a query with a left outer join just like below, however Access is showing a warning dialog "JOIN expression not supported".I understand that Access doesn't support INNER JOIN nested inside a LEFT JOIN but as I am a beginner in SQL I don't see any other way to get the same result.
The goal of the query is to get everything that is in the select even when InvoiceItems.Amount is null.
SELECT MainOrder.OrderNumber, OrderComponent.ArticleNumber, SupplierOrderMain.*, InvoiceItems.Amount
FROM InvoiceItems LEFT JOIN
((MainOrder INNER JOIN
OrderComponent
ON MainOrder.OrderNumber = OrderComponent.OrderNumber
) INNER JOIN
SupplierOrderMain
ON OrderComponent.ID = SupplierOrderMain.OrderComponentID
)
ON InvoiceItems.OrderComponent = OrderComponent.ID;
I'm not sure why you would want outer joins in this situation at all (you don't explain why). But just start with the table where you want to keep everything and work from there:
SELECT MainOrder.OrderNumber, OrderComponent.ArticleNumber, SupplierOrderMain.*, InvoiceItems.Amount
FROM ((InvoiceItems LEFT JOIN
OrderComponent
ON InvoiceItems.OrderComponent = OrderComponent.ID
) LEFT JOIN
MainOrder
ON MainOrder.OrderNumber = OrderComponent.OrderNumber
) LEFT JOIN
SupplierOrderMain
ON OrderComponent.ID = SupplierOrderMain.OrderComponentID

Updating upon a join

I can simply use a select statment on a join to pull results I'd Like using:
Select * from RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, is there a convenient way which I could do an update on the selected fields?
I have tried this so far:
Update RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
set Results.S = 'OOS-B'
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, it is passing an error indicating "Incorrect syntax near '('"
Is there a way for me to update off of this join or will I need to do tables individually?
Your select syntax suggests SQL Server. The correct update syntax in SQL Server is:
update r
set S = 'OOS-B'
from results r left join
orders o
on r.ordno = o.ordno left join
folders f
on f.folderno = o.folderno left join
pranaparms p
on f.prodcode = p.prodcode and r.analyte = p.analyte
where r.s <> 'OOS-A' and
r.Final Between p.LOWERQCLIMIT and p.UPPERQCLIMIT and
(p.LOWERQCLIMIT IS NOT NULL and p.UPPERQCLIMIT IS NOT NULL) and
r.ordno in (1277494);
Notes:
Table aliases make the query much easier to write and to read.
Do not use NOLOCK unless you know what you are doing. Given that you don't know the syntax rules for update for the database you are using, I will guess that you don't understand locks either.
Your WHERE conditions are turning the outer joins into inner joins. You should just specify the correct join type -- and probably move the conditions to the on clauses. I've left the logic as you wrote it.

Is there a better way to write this Oracle SQL query?

I have been using Oracle SQL for around 6 months so still a beginner. I need to query the database to get information on all items on a particular order (order number is via $_GET['id']).
I have come up with the below query, it works as expected and as I need but I do not know whether I am over complicating things which would slow the query down at all. I understand there are a number of ways to do a single thing and there may be better methods to write this query since I am a beginner.
I am using Oracle 8i (due to this is the version an application we use is supplied with) so I believe that some JOIN etc. are not available in this version, but is there a better way to write a query such as the below?
SELECT auf_pos.auf_pos,
(SELECT auf_stat.anz
FROM auf_stat
WHERE auf_stat.auf_pos = auf_pos.auf_pos
AND auf_stat.auf_nr = ".$_GET['id']."),
(SELECT auf_text.zl_str
FROM auf_text
WHERE auf_text.zl_mod = 0
AND auf_text.auf_pos = auf_pos.auf_pos
AND auf_text.auf_nr = ".$_GET['id']."),
(SELECT glas_daten_basis.gl_bez
FROM glas_daten_basis
WHERE glas_daten_basis.idnr = auf_pos.glas1),
(SELECT lzr_daten.lzr_breite
FROM lzr_daten
WHERE lzr_daten.lzr_idnr = auf_pos.lzr1),
(SELECT glas_daten_basis.gl_bez
FROM glas_daten_basis
WHERE glas_daten_basis.idnr = auf_pos.glas2),
auf_pos.breite,
auf_pos.hoehe,
auf_pos.spr_jn
FROM auf_pos
WHERE auf_pos.auf_nr = ".$_GET['id']."
Thanks in advance to any Oracle gurus that could help this beginner out!
You could rewrite it using joins. If your subselects aren't expected to return any NULL values, then you can use INNER JOINS:
SELECT auf_pos.auf_pos,
auf_stat.anz,
auf_text.zl_str,
glas_daten_basis.gl_bez,
lzr_daten.lzr_breite,
glas_daten_basis.gl_bez,
auf_pos.breite,
auf_pos.hoehe,
auf_pos.spr_jn
FROM auf_pos
INNER JOIN auf_stat ON auf_stat.auf_pos = auf_pos.auf_pos AND auf_stat.auf_nr = ".$_GET['id'].")
INNER JOIN auf_text ON auf_text.zl_mod = 0 AND auf_text.auf_pos = auf_pos.auf_pos AND auf_text.auf_nr = ".$_GET['id'].")
INNER JOIN glas_daten_basis ON glas_daten_basis.idnr = auf_pos.glas1
INNER JOIN lzr_daten ON lzr_daten.lzr_idnr = auf_pos.lzr1
INNER JOIN glas_daten_basis ON glas_daten_basis.idnr = auf_pos.glas2
Or if there are cases where you wouldn't have matches on all the tables, you could replace the INNER joins with LEFT OUTER joins:
SELECT auf_pos.auf_pos,
auf_stat.anz,
auf_text.zl_str,
glas_daten_basis.gl_bez,
lzr_daten.lzr_breite,
glas_daten_basis.gl_bez,
auf_pos.breite,
auf_pos.hoehe,
auf_pos.spr_jn
FROM auf_pos
LEFT OUTER JOIN auf_stat ON auf_stat.auf_pos = auf_pos.auf_pos AND auf_stat.auf_nr = ".$_GET['id'].")
LEFT OUTER JOIN auf_text ON auf_text.zl_mod = 0 AND auf_text.auf_pos = auf_pos.auf_pos AND auf_text.auf_nr = ".$_GET['id'].")
LEFT OUTER JOIN glas_daten_basis ON glas_daten_basis.idnr = auf_pos.glas1
LEFT OUTER JOIN lzr_daten ON lzr_daten.lzr_idnr = auf_pos.lzr1
LEFT OUTER JOIN glas_daten_basis ON glas_daten_basis.idnr = auf_pos.glas2
Whether or not you see any performance gains is debatable. As I understand it, the Oracle query optimizer should take your query and execute it with a similar plan to the join queries, but this is dependent on a number of factors, so the best thing to do it give it a try..

Ambiguous outer join in MS Access

Trying to create an outer join on two other joined tables when recieving this error - I just dont see how to create two separate queries to make it work. Subqueries don't seem to work either, any help appreciated. I get errors for the below query, thanks.
SELECT
CardHeader.CardID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin, CardDetail.ItemID, Items.ItemDescription,
Items.VCatalogID, CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost, CardColors.ColorID
FROM
((Items
INNER JOIN
(CardHeader INNER JOIN CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID)
LEFT JOIN
CardColors ON CardDetail.ItemID = CardColors.ItemID)
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID
ORDER BY
CardHeader.CardID;
I tried the following which runs but asks for the following parameters (which it shouldnt)
CardHeader.ID, MainQry.CardID
SELECT
MainQry.ID, MainQry.CardDescription, MainQry.GloveSize,
MainQry.GloveDescription, MainQry.Bin, MainQry.ItemID,
MainQry.ItemDescription, MainQry.VCatalogID, MainQry.ChargeCode,
MainQry.Quantity, MainQry.Cost, SubQry.ColorID
FROM
(SELECT
CardHeader.ID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin,
CardDetail.ItemID, Items.ItemDescription, Items.VCatalogID,
CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost
FROM
Items
INNER JOIN
(CardHeader
INNER JOIN
CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID
) AS MainQry
LEFT JOIN
(SELECT
CardColors.ItemID, CardColors.ColorID
FROM
CardColors
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID) AS SubQry ON MainQry.ItemID = SubQry.ItemID
ORDER BY
MainQry.CardID;
The second SQL statement can be corrected by reference to the first statement and the error. The error is that both CardHeader.ID and MainQry.CardID are prompting for a parameter, which indicates that the inner statement should include CardHeader.CardID, rather than CardHeader.ID

SQL Server 2008 error 4145

I have used following code there in SQL
SELECT
Stu_info.Id_num,
Stu_info.Stu_name,
Development_fee.Dvf,
Tuition_fee.Acy,
Tuition_fee.Tui_fee,
Registration_fee.Reg_fee,
Form_fill_up_fee.Acy,
Form_fill_up_fee.FFF,
Examination_fee.E_typ,
Examination_fee.Exm_fee,
monthly_instal.Instm,
monthly_instal.Paid
FROM
[SUIMT].[dbo].[Stu_info],
[SUIMT].[dbo].[Development_fee],
[SUIMT].[dbo].[Tuition_fee],
[SUIMT].[dbo].[Registration_fee],
[SUIMT].[dbo].[Form_fill_up_fee],
[SUIMT].[dbo].[Examination_fee],
[SUIMT].[dbo].[monthly_instal]
WHERE
Development_fee.Id_num,
Tuition_fee.Id_num,
Registration_fee.Id_num,
Form_fill_up_fee.Id_num,
Examination_fee.Id_num,
monthly_instal.Id_num = Stu_info.Id_num
but it shows error which says
"Msg 4145, Level 15, State 1, Line 3
An expression of non-boolean type specified in a context where a
condition is expected, near ','".
Would you please help me to solve this problem? I would be grateful for providing an example so I could understand it easier.
What is the WHERE clause meant to achieve? Do you mean this?
WHERE
Stu_info.Id_num = Development_fee.Id_num
AND Stu_info.Id_num = Tuition_fee.Id_num
AND Stu_info.Id_num = Registration_fee.Id_num
AND Stu_info.Id_num = Form_fill_up_fee.Id_num
AND Stu_info.Id_num = Examination_fee.Id_num
AND Stu_info.Id_num = monthly_instal.Id_num
Also, using FROM tableA, TableB, TableC is a very old style notation. ANSI-92 standard uses JOIN...
SELECT
Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid
FROM
[SUIMT].[dbo].[Stu_info]
INNER JOIN
[SUIMT].[dbo].[Development_fee]
ON Stu_info.Id_num = Development_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Tuition_fee]
ON Stu_info.Id_num = Tuition_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Registration_fee]
ON Stu_info.Id_num = Registration_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Form_fill_up_fee]
ON Stu_info.Id_num = Form_fill_up_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[Examination_fee]
ON Stu_info.Id_num = Examination_fee.Id_num
INNER JOIN
[SUIMT].[dbo].[monthly_instal]
ON Stu_info.Id_num = monthly_instal.Id_num
You should use proper ANSI SQL-92 JOIN syntax (INNER JOIN, LEFT OUTER JOIN etc.) and table alias to make your query more understandable and easier to read - something like:
SELECT
stu.Id_num, stu.Stu_name,
df.Dvf,
tf.Acy, tf.Tui_fee,
rf.Reg_fee,
ffuf.Acy, ffuf.FFF,
ef.E_typ, ef.Exm_fee,
mi.Instm, mi.Paid
FROM
[SUIMT].[dbo].[Stu_info] stu
INNER JOIN
[SUIMT].[dbo].[Development_fee] df ON df.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Tuition_fee] tf ON tf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Registration_fee] rf ON rf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Form_fill_up_fee] ffuf ON ffuf.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[Examination_fee] ef ON ef.Id_num = stu.id_num
INNER JOIN
[SUIMT].[dbo].[monthly_instal] mi ON mi.Id_Num = stu.Id_num
This way, you don't even need an ugly WHERE clause at all.....
Separate your clauses in where by AND or OR, not a comma
SELECT Stu_info.Id_num, Stu_info.Stu_name, Development_fee.Dvf, Tuition_fee.Acy, Tuition_fee.Tui_fee, Registration_fee.Reg_fee, Form_fill_up_fee.Acy, Form_fill_up_fee.FFF, Examination_fee.E_typ, Examination_fee.Exm_fee, monthly_instal.Instm, monthly_instal.Paid
FROM [SUIMT].[dbo].[Stu_info],
[SUIMT].[dbo].[Development_fee],
[SUIMT].[dbo].[Tuition_fee],
[SUIMT].[dbo].[Registration_fee],
[SUIMT].[dbo].[Form_fill_up_fee],
[SUIMT].[dbo].[Examination_fee],
[SUIMT].[dbo].[monthly_instal]
WHERE Development_fee.Id_num=Tuition_fee.Id_num and
Registration_fee.Id_num=Form_fill_up_fee.Id_num and
Examination_fee.Id_num=monthly_instal.Id_num and
monthly_instal.Id_num=Stu_info.Id_num