SQL Query error with Join - sql

When I try to execute this SQL query, I get an error and I don't know how to correct it
select *
from pg_specialites_annees pgs
where pgs.NOM_FR like '%tets%'
left join pg_specialites_parametres pgsp on pgsp.ID_SPECIALITE = pgs.ID_SPECIALITE;
When I remove the LIKE it works, but I need a specific row.
Please help me, I don't know how to do it. I want to show some details for a specific row in table from another table.

Your LEFT JOIN needs to come before the WHERE:
Select *
From pg_specialites_annees pgs
Left Join pg_specialites_parametres pgsp on pgsp.ID_SPECIALITE = pgs.ID_SPECIALITE
Where pgs.NOM_FR Like '%tets%';

Put your where clause after the join clause :
select * from pg_specialites_annees pgs
left join pg_specialites_parametres pgsp on pgsp.ID_SPECIALITE = pgs.ID_SPECIALITE;
where pgs.NOM_FR like '%tets%'

Related

SQL JOIN with the same table

I am trying to query in SQL and I can not resolve it.
I have a table tCliente:
What I want to do is a JOIN with the same table to find each pair of clients that lives in the same city.
I try to do this:
SELECT DISTINCT c.codiClien, c.nombreClien, c1.codiClien, c1.nombreClien, c.ciudadClien
FROM tCliente c
INNER JOIN tCliente c1 ON c.ciudadClien = c1.ciudadClien
and get this:
But I should get this:
I know I have to filter data, but I have tried many things and I can not find the solution.
Also, I tried to use GROUP BY but it is not possible. I wanted to group by pair, that is, something like this:
...
GROUP BY c.codiClien, c1.codiClien
But in doing so I get errors in the query. Could someone please help me? Thanks
Note:
When using ON in the INNER JOIN, I would like to know if it is "possible" to do that or should not do it, because the usual thing is to do tb1.id = tb2.id
You must exclude itself on the inner join.
SELECT c.codiClien, c.nombreClien, c1.codiClien, c1.nombreClien, c.ciudadClien
FROM tCliente c
INNER JOIN tCliente c1
ON c.ciudadClien = c1.ciudadClien
AND c.codiClien < c1.codiClien;

MS Access, how to replace a saved query by the full query in a subquery

I have 3 tables:
1 - tblMembers_Info
2 - a junction table
3 - tblCourses
I need to query the members who haven't done a specific course.
After trying to do it manually I gave MS Access "Query Wizard" a try. I ended up with :
A saved query as Query1:
// That one query who did the course
SELECT tblMembers_Info.*, tblCourses.CourseName
FROM tblMembers_Info
INNER JOIN
(tblCourses INNER JOIN tblMembers_Courses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID
WHERE (tblCourses.CourseName) In ('NameOftheCourse');
2nd query using the saved Query1:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
LEFT JOIN [Query1]
ON tblMembers_Info.[Members_ID] = Query1.[Members_ID]
WHERE (((Query1.Members_ID) Is Null));
How can I replace the Query1 in the second query with the full query instead of using a QueryDef (the saved query "Query1")?
Also, there's a better way for sure to write that query, I would really appreciate any help.
You can simply replace LEFT JOIN [Query1] with LEFT JOIN (...) AS [Query1] where ... should be the SQL of the first query, without the ending ;.
But I think in your specific case the use of NOT IN might give a better performance to get the same results:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
WHERE tblMembers_Info.[Members_ID] NOT IN (
SELECT tblMembers_Info.[Members_ID]
FROM ((tblMembers_Info
INNER JOIN tblMembers_Courses
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID)
INNER JOIN tblCourses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
WHERE tblCourses.CourseName = 'NameOftheCourse'
);

Join expression not supported SQL

SELECT
Trs.itemID, Trs.imtName, Trs.sumQty, Sum(whiQty)
FROM
((SELECT
trsitemID AS itemID, trsimtName AS imtName,
Sum(trsQty) As sumQty
FROM
tblTransactionSub AS T
WHERE
trstraID = 1231
AND trsActive = True
GROUP BY
trsitemID, trsimtName) AS Trs
INNER JOIN
tblWarehouseItem AS WHI ON Trs.itemID = WHI.whiitemID)
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
WHERE
whiActive = True
AND whiCansel = False
AND whiwrhID = 19
GROUP BY
Trs.itemID,Trs.imtName, Trs.sumQty
HAVING
SUM(whiQty) < Trs.sumQty
If you please help me me out since I am new to SQL commands I can not easily find my mistake.
Thanks in advance
The error that occurred when I added the Right Join is:
Join expression not supported
In MS Access, you have to use parenthesises with multiple joins:
select ...
from
((table1
... join table2 on ...)
... join table3 on ...)
... join tableN
/edit/
As OP question changes its syntax often, then my answer seems out of place :) Initially there were no parens there.
About RIGHT JOIN: You need to use table name (or entire subselect) after JOIN keyword, not skip it or use some other alias. Your query part
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
currently uses alias WHI, which is wrong in two ways: 1) it is not table name 2) it is already used. You need something like this:
RIGHT JOIN
tblWarehouseItem AS WHI2 ON Trs.trswhiID = WHI2.whiID
It could be possible that MS Access restricts your kind of JOINs usage (like INNER join should not come after LEFT join); I have currently no possibility to check precise rules.
Your problem is that you have no table name after the RIGHT JOIN:
RIGHT JOIN
ON Trs.trswhiID = WHI.whiID
Should be:
RIGHT JOIN YOURTABLENAMEHERE as alias
ON Trs.trswhiID = WHI.whiID
However, you have already defined Trs and Whi, so I have no idea what table you want there, or why. Perhaps you just want to change the INNER JOIN to a LEFT JOIN or RIGHT JOIN.

4 TABLES INNER JOIN SQL STATEMENT

I have this database structure...
(It´s on spanish, hope doesn´t matter) and I made this query using inner join but I don´t get any result so I guess I did something wrong... This is my query...
SELECT TBoleta.NroSerie+'-'+TBoleta.NroBoleta Boleta, TBoleta.Fecha,
TAlumno.APaterno+' '+TAlumno.AMaterno+' '+TAlumno.Nombres as Apellidos_y_Nombres,
TGrupoModulo.Modulo + ' ' + TGrupoModulo.Seccion + ' ' + TGrupoModulo.Turno,
TBoleta.Monto
FROM TMatricula
inner join TAlumno on TMatricula.CodAlumno = TAlumno.CodAlumno
inner join TBoleta on TBoleta.NroMatricula = TMatricula.NroMatricula
inner join TGrupoModulo on TGrupoModulo.CodGrupoModulo = TMatricula.CodGrupoModulo
Please... I will appreciate any help. Thanks!
For starters, this attribute doesn't exist; TBoleta.CodAlumno
Edit: Now that your query appears to be compiling fine, remember that inner joins necessitate data from each join to have matching values. If one of the joins don't, you get no data back. Analyze your data to figure this out. You could also do left outer joins at first, then move to inner joins step by step.
This is just a cursory guess, but this line:
inner join TAlumno on TBoleta.CodAlumno = TAlumno.CodAlumno
appears to be incorrect, because TBoleta doesn't have a column named CodAlumno. You should check to see if you're getting any errors whilst running this query.
If any field column is empty, then result is not displayed.
further if you want to display with empty or null values you can use LEFT JOIN in your query

Basic SQL join question. Can you help me improve my skillset?

Ok.. So I'm trying to improve my SQL skills and have a question. Here is a screen shot of the schema.
Schema http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif
(http://img509.imageshack.us/img509/97/screenhunter02nov121946.gif)
Alright so I'm selecting a bunch of Report Bundles and other rows from a table you can see. I've got these two tables joining together correctly and displaying what should be returned. Now I need to add another field onto my result rows that states what type of report this is. How can I join up to the ReportGroupType table through the ReportBundleGroup table without getting a shwack of results?
Here is the query I am using so far.
SELECT *
FROM ReportBundleCustomerVisibility INNER JOIN ReportBundle
ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
Thanks again SO
SELECT *
FROM ReportBundleCustomerVisibility AS v
JOIN ReportBundle AS b ON b.ID = v.ReportBundleID
JOIN ReportBundleGroup AS g ON b.ID = g.ReportBundleID
JOIN ReportGroupTYpe AS t ON t.ID = g.ReportGroupTypeID
WHERE v.ReferenceCustomerID = 2303
It sounds like you just need another inner join to get the information you need. You can think about the second join as joining the result of the join with the ReportGroupType table. I added parenthesis to try to join the two sets the second INNER JOIN is operating on.
SELECT * FROM ((ReportBundleCustomerVisibility
INNER JOIN ReportBundle ON ReportBundleCustomerVisibility.ReportBundleID = ReportBundle.ID)
INNER JOIN ReportGroupType ON ReportBundleGroup.ReportGroupTypeID = ReportGroupType.ID)
WHERE ReportBundleCustomerVisibility.ReferenceCustomerID = 2303
I also highly suggest against using "SELECT *" in production code or any query you plan on reusing as the table schema can change and possibly effect reports and UI. Explicitly specify the columns instead.