Access delete query with an inner join and subquery - sql

I am trying to run a delete query that contains a subquery and an inner join and get the error
Query must have at least one destination field.
I have read elsewhere that you must use either of the following.
DISTINCTROW
WHERE EXISTS
But I still get the error message. I have read How to delete in MS Access when using JOIN's? and MS ACCESS delete query syntax combined with inner join problems but can't get it to work.
Here is my query.
DELETE FROM tblUp
WHERE NOT EXISTS (
SELECT tblUp.request_id,
tblUp.gid,
MAX(tblUp.savings_year) AS MaxOfsavings_year,
LAST(tblUp.savings_month) AS LastOfsavings_month
FROM tblUp
INNER JOIN tbl_w ON tblUp.request_id = tblW.id
GROUP BY tblUp.request_id,
tblUp.gid,
tblW.Cde
HAVING (((tblW.Cde)="ML"))
ORDER BY tblUp.request_id,
tblUp.gid,
MAX(tblUp.savings_year),
LAST(tblUp.savings_month)
)

Related

Prevent duplicate record when inner join query in SQL

I used the inner join command to get the data from two tables.
But, when I run the SQL query.
I got the same record duplicated 48 times.
The SQL query I created is below
SELECT
ABS_LIMIT.B1_NAME, ABS_LIMIT.B2_NAME, ABS_LIMIT.B3_NAME, ABS_LIMIT.ELEM_NAME
FROM
ABS_LIMIT
INNER JOIN
RTU_SCAN ON RTU+SCAN.B1_NAME = ABS_LIMIT.B1_NAME
WHERE
ABS_LIMIT.B3_NAME LIKE 'AMP%';
Does anyone have any idea how to remove the duplicate from the query result?
You never SELECT any columns from RTU_SCAN so you can use EXISTS rather than an INNER JOIN:
SELECT a.B1_NAME,
a.B2_NAME,
a.B3_NAME,
a.ELEM_NAME
FROM ABS_LIMIT a
WHERE EXISTS (SELECT 1 FROM RTU_SCAN r WHERE r.B1_NAME = a.B1_NAME)
AND a.B3_NAME LIKE 'AMP%';
Then, if there are duplicates in RTU_SCAN they will not propagate duplicate rows in the output.
Alternatively, you could use DISTINCT to remove duplicates:
SELECT DISTINCT
a.B1_NAME,
a.B2_NAME,
a.B3_NAME,
a.ELEM_NAME
FROM ABS_LIMIT a
INNER JOIN RTU_SCAN r
ON r.B1_NAME = a.B1_NAME
AND a.B3_NAME LIKE 'AMP%';
However, it will probably be less efficient to generate duplicates and then filter them out using DISTINCT compared to using EXISTS and not generating the duplicates in the first place.

Syntax error on join operation access SQL

I'm trying to do some join operations in sql access but I keep on getting the join operation error. At first it was just the JOIN alone, but then I realized I had to add the INNER which I did but it didn't resolve the error.
Code below:
SELECT Formula.*, Ingred.[Europe Ban]
FROM [Ingred]
INNER JOIN Ingred ON Formula.[Ingredient] = Ingred.Ingredients;
Presumably, you want Formula in the FROM clause, not Ingred twice:
SELECT Formula.*, Ingred.[Europe Ban]
FROM Formula INNER JOIN
Ingred
ON Formula.[Ingredient] = Ingred.Ingredients;

How to get delete query to work in MS Access

I have set up a subquery to select my records and then the delete query to perform the action. When I run it, I get an error message saying:
Could not delete from specified tables
Here my SQL code in the delete query:
PARAMETERS UnitID Short;
DELETE DISTINCTROW qry_exp_comments_select.*
FROM qry_exp_comments_select;
And the called subquery:
PARAMETERS UnitID Short;
SELECT tbl_Comments.*, tbl_Activity.ActivityID
FROM tbl_BusUnits INNER JOIN (tbl_Activity INNER JOIN tbl_Comments ON tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON tbl_BusUnits.UnitID = tbl_Activity.UnitID
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));
Why won't the query work? I have tried to set it up as the one in the following thread: MS ACCESS delete query syntax combined with inner join problems
In order to delete all the records in tbl_Comments and tbl_Activity all I had to do was change the relationships of the tables so that they have Cascade Delete activated.
Afterwards a simple delete query on the tbl_BusUnits also deleted all associated records in the other tables.
Code for the Delete Query as follows:
PARAMETERS UnitID Short;
DELETE DISTINCTROW tbl_BusUnits.UnitID
FROM tbl_BusUnits
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));

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'
);

Access SQL syntax error (missing operator) when query contains multiple JOINs

I'm trying to make an UPDATE query (using Access 2013) that calculates a value based on values stored in two separate linked tables. Here is the code I'm using:
UPDATE tblCreatures
INNER JOIN tblRole ON tblCreatures.Role = tblRole.RoleName
INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);
This gives me a syntax error, saying
Syntax error (missing operator) in query expression "tblCreatures.Role = tblRole.RoleName INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankNam"
(and yes, it cuts off at RankNam, not RankName)
Testing things out, if I remove one of the inner joins (and thus all references to that table) then the update query works just fine, but if I put the other inner join back in, I continuously get this same syntax error. I don't understand why... I should be able to put two inner joins next to each other, shouldn't I?
Access SQL requires parentheses when a query contains multiple JOINs. If you build the query in Access' query designer it will look like this (reformatted for clarity):
UPDATE
(
tblCreatures
INNER JOIN
tblRole
ON tblCreatures.Role = tblRole.RoleName
)
INNER JOIN
tblRank
ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);