Have tried the below SQL in MS Access but cannot seem to get it working, anyone got a better idea?
SELECT top 4 Student.STUDENT_DEGREE, Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM (((Student)
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;
Whenever i run this i just get the error:
Syntax error in FROM clause
Group is a reserved word. Enclose that name in square brackets to avoid confusing the db engine. You can also assign an alias for the table name.
FROM
(Student
INNER JOIN Result
ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN [Group] AS g
ON Result.RESULT_GROUP_ID = g.GROUP_ID
I had to guess which tables contain those fields in the last ON clause. If you set up the joins in Design View of the Access query designer, it will help you get the names right. It will also add the parentheses which the db engine requires for any query which includes more than one join.
Also qualify the table sources for the field names in your SELECT list and elsewhere in the query. Here again, the query designer can supply the correct names for you.
Remove the extra set of parentheses around Student:
SELECT top 4 Student.STUDENT_DEGREE,Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM ((Student
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;
Related
I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.
I am trying to create a query with the top 3 shops that have the highest average revenue amount and retail_type being fashion and shoes. I have to join 3 tables where I have to join 2 to the 1st one. Something seems to be wrong with the syntax below:
Select * from (Select
a.shop_id,
c.retail_type,
avg(b.revenue_amount) as avg_revenue_amount
From
shops a
Left join rev b on a.shop_id = b.shop_id
Left join trans c on b.trans_id = c.trans_id
Where
c.retail_type in (‘fashion’, ‘ shoes ’)
Group by
1,
2
Order by
avg_revenue_amount desc)
limit 10;
I would recommend writing the query as:
select s.shop_id, t.retail_type,
avg(r.revenue_amount) as avg_revenue_amount
from shops s join
rev r
on s.shop_id = r.shop_id join
trans
t
on r.trans_id = t.trans_id
where t.retail_type in ('fashion', 'shoes')
group by s.shop_id, t.retail_type
order by avg_revenue_amount desc
limit 10;
You don't specify your database, but the following are potentially problematic:
Bad quotes in the in clause.
Lack of alias after the derived table.
Use of numeric placeholders in GROUP BY.
Not all of these are necessarily errors, but they could be depending on your database.
The following would not cause a syntax error but might cause other problems:
Spaces around a string for an IN comparison means the comparison might not work.
Use of arbitrary letters for table aliases makes the query really hard to follow.
The WHERE clause turns the LEFT JOINs to inner joins so LEFT JOINs are highly misleading.
The subquery is superfluous.
You have extra bracket on the line:
avg_revenue_amount) desc)
Also you have typo on the second line: a.shop.id -> a.shop_id and another problem with quotes: ‘fashion’, ‘ shoes ’ -> 'fashion', 'shoes'
Check edited query on the SQL Fiddle.
I want to do self-join I have written a code as well but its throwing an error and it seems that there is some issue with an alias.
Apart from this it would also be helpful for me if someone let me know any best site where I can learn query in MS Access. I searched a number of places everywhere, it is showing through UI Interface but I want to learn query in MS Access.
(SELECT distinct itemname,vendorname,price,count(*)
from vendor_Details1
group by itemname,vendorname,price
order by vendorname) A
inner join
(SELECT distinct itemname,vendorname,price,count(*)
from vendor_Details1
group by itemname,vendorname,price
order by vendorname) B
on A.vendorname=B.vendorname
It is entirely unclear what you are trying to do. However, "join" is an operator in the FROM clause that operates on two tables, views, or subqueries.
The structure of a self-join looks like:
select . . . -- list of columns here
from t as t1 inner join -- you need aliases for the table so you can distinguish the references
t as t2
on t1.? = t2.? -- the join condition goes here
Your query doesn't even have a select.
I have been trying to figure out why the following SQL works
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID=1);
But when I try to use the aliasname (A_ID) in the WHERE clause, I got an error
SELECT c_Supplier.Supplier_ID AS A_ID, c_Supplier.Name, c_Supplier.RFC, c_Supplier_Direccion.Description, c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN (c_Supplier_Direccion LEFT JOIN c_Supplier_Phone ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (A_ID=1);
Any ideas?
I don't understand your question. This is a reasonably formed SQL query:
SELECT c_Supplier.Supplier_ID AS Entidad_ID, c_Supplier.Name,
c_Supplier.RFC, c_Supplier_Direccion.Description,
c_Supplier_Direccion.Address, c_Supplier_Phone.Phone
FROM c_Supplier LEFT JOIN
(c_Supplier_Direccion LEFT JOIN
c_Supplier_Phone
ON c_Supplier_Direccion.Supplier_Direccion_ID = c_Supplier_Phone.Supplier_Direccion_ID
) ON c_Supplier.Supplier_ID = c_Supplier_Direccion.Supplier_ID
WHERE (c_Supplier.Supplier_ID = 1);
(I would recommend table aliases for readability, but that is a separate issue.)
It has no alias called A_ID anywhere in the query, so there is no reason to ever expect a reference to A_ID to work (unless it is a column in one of the tables).
And, SQL doesn't allow the re-use of table aliases in the SELECT where they are defined or the WHERE clause. This is not an MS Access limitation; it is how the SQL language is defined.
If you want to do so in MS Access, you can use a subquery and reference the table alias in the outer query.
Just need to ask for your help, I have been having some problems with MS Access, I'm trying to create a nested INNER JOIN to perform a query. All of the fields that I need are showing up, however when I try to add a new entry it gives me an error
Can't Add Records Join key of table is not in record set.
Here's my code:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
(Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number)
INNER JOIN
Table_JuniorRecruiter ON Table_Applicant.Junior_Recruiter_ID = Table_JuniorRecruiter.Junior_Recruiter_ID;
The brackets you have round the first table and its first inner join give a syntax error. Between the ( and the ) needs to be a complete SELECT statement, and you start off with a table name, which no valid SELECT statement will do.
Now I don't actually know what you're trying to do, but this will be valid SQL:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number
INNER JOIN Table_JuniorRecruiter
ON Table_Applicant.Junior_Recruiter_ID
= Table_JuniorRecruiter.Junior_Recruiter_ID
Here I've just taken out the ( and the ). It should be valid SQL (provided the table and column names are correct) but it might not be the actual query you want.
However, I can't see what you'd gain by making the table_applicant and table_exam join a nested subquery: doing that looks totally un-necessary.