MS Access error, aggregate function - sql

I am trying to run the sql query below in access. I keep getting the MS Access error:
"You tried to execute a query that does not include the specified expression 'ID' as part of an aggregate function"
The query I wrote is as below:
SELECT Subject.ID, Subject.Description, Max(DataSets.ID) AS ID_DataSets
FROM Subject
INNER JOIN DataSets ON Subject.Description = DataSets.Subject.Value
GROUP BY Subject.ID, Subject.Description;

I see an error in this reference DataSets.Subject.Value, but I'm not sure it would generate that error message. Try this version:
SELECT s.ID, s.Description, Max(ds.ID) AS ID_DataSets
FROM Subject as s INNER JOIN
DataSets as ds
ON s.Description = ds.Value -- is this correct?
GROUP BY s.ID, s.Description;
I find queries easier to write and read when they use table aliases.

Related

PostgreSQL - aggregate functions are not allowed in JOIN conditions

How to fix this error
Error in query: ERROR: aggregate functions are not allowed in JOIN conditions
this my query
SELECT *
FROM "downloads" dl
inner join access_logs l
on dl.id=l.download_id
and dl.file_id=l.file_id
and dl.site_id=l.site_id
and sum(l.body_bytes_Sent)>dl.filesize
I want the list of all files downloaded completely.
sum(body_bytes_Sent)>=filesize
Seems you are assuming that somehow SQL will know that it should sum somthing - but is has not received any instructions on how to perform the aggregation which requires a group by clause. So, form a complete query and then join it as a "derived table" like this:
SELECT *
FROM downloads dl
inner join (
SELECT download_id, file_id, site_id, sum(l.body_bytes_Sent) AS sum_sent
FROM access_logs
GROUP BY download_id, file_id, site_id
) AS l
on dl.id=l.download_id
and dl.file_id=l.file_id
and dl.site_id=l.site_id
and L.sum_Sent > dl.filesize
As there are no details to hand on the data model, or sample data to inspect, the query above may still require refinement.

Access SQL Syntax error: missing operator

I am trying to convert a T-SQL query to MS Access SQL and getting a syntax error that I am struggling to find. My MS Access SQL query looks like this:
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal )
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
MyParameterSettings on 1=1].ProdClass
INNER JOIN
[SalesTerritoryFilter_Check all that apply] ON IndvMast.SalesTerr = [SalesTerritoryFilter_Check all that apply].SalesTerr
WHERE
(((OHdrMast.OrdDate) >= [MyParameterSettings].[RFM_StartDate]))
GROUP BY
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal,
[CustTypeFilter_Check all that apply].IncludeInRFM,
[ProductClassFilter_Check all that apply].IncludeInRFM,
[SourceCodeFilter_Check all that apply].IncludeInRFM,
IndvMast.FlgDontUse
I have reviewed differences between MS Access SQL and T-SQL at http://rogersaccessblog.blogspot.com/2013/05/what-are-differences-between-access-sql.html and a few other locations but with no luck.
All help is appreciated.
update: I have removed many lines trying to find the syntax error and I am still getting the same error when running just (which runs fine using T-SQL):
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
[My Parameter Settings] ON 1 = 1
There are a number of items in your query that should also have failed in any SQL-compliant database:
You have fields from tables in GROUP BY not referenced in FROM or JOIN clauses.
Number of fields in SELECT query do not match number of fields in INSERT INTO clause.
The MyParameterSettings table is not properly joined with valid ON expression.
Strictly MS Access SQL items:
For more than one join, MS Access SQL requires paired parentheses but even this can get tricky if some tables are joined together and their paired result joins to outer where you get nested joins.
Expressions like ON 1=1 must be used in WHERE clause and for cross join tables as MyParameterSettings appears to be, use comma-separated tables.
For above reasons and more, it is advised for beginners to this SQL dialect to use the Query Design builder providing table diagrams and links (if you have the MS Access GUI .exe of course). Then, once all tables connect graphically with at least one field selected, jump into SQL view for any nuanced scripting logic.
Below is an adjustment to SQL statement to demonstrate the parentheses pairings and for best practices, uses table aliases especially with long table names.
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal)
SELECT
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
FROM
[MyParameterSettings] p, (IndvMast i
INNER JOIN
OHdrMast o ON i.IndvID = o.IndvID)
INNER JOIN
[SalesTerritoryFilter_Check all that apply] s ON i.SalesTerr = s.SalesTerr
WHERE
(o.OrdDate >= p.[RFM_StartDate])
GROUP BY
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
And in your smaller SQL subset, the last table does not need an ON 1=1 condition and may be redundant as well in SQL Server. Simply a comma separate table will suffice if you intend for cross join. The same is done in above example:
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
[My Parameter Settings], IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
I suppose there are some errors in your query, the first (more important).
Why do you use HAVING clause to add these conditions?
HAVING (((IndvMast.IndRecency)>(date()-7200))
AND (([CustTypeFilter_Check all that apply].IncludeInRFM)=1)
AND (([ProductClassFilter_Check all that apply].IncludeInRFM)=1)
AND (([SourceCodeFilter_Check all that apply].IncludeInRFM)=1)
AND ((IndvMast.FlgDontUse) Is Null))
HAVING usually used about conditions on aggregate functions (COUNT, SUM, MAX, MIN, AVG), for scalar value you must put in WHERE clause.
The second: You have 12 parenthesis opened and 11 closed in HAVING clause

Syntax error (missing operator) when using UPDATE with INNER JOIN

I'm trying to execute this SQL query on an MS Access DB:
UPDATE WW_B
SET WW_B.WWtype_ID=1
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr;
But I get the following error:
[Microsoft][ODBC Microsoft Access Driver]
Syntax error (missing operator) in expression 1 INNER JOIN New_data on WW_B.StdNr = New_data.StdNr.
I don't see where any operator is needed, since I don't use any parentheses or quotation marks.
I've also tried WWtype_ID='1' and WWtype_ID="1" and got the same error.
What am I doing wrong?
I was having the same problem and found this question while searching for an answer. Luckily I was able to find a solution while digging through some Access queries at work.
This is just another situation where MS Access does not play nice with standard SQL syntax.
The other answers are incorrect. You do not need a FROM clause, Access will just give you the same error message. Where you ran into the error was where you placed the JOIN. It seems intuitive that you would have FROM...JOIN... But this is MS Access where working with SQL is never intuitive.
In Access, UPDATE seems to take the place of FROM. Therefore, you add the JOIN statement to the end of the UPDATE clause.
UPDATE WW_B
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr
SET WW_B.WWtype_ID=1;
You can also add a WHERE clause after the SET statement if desired.
You need a FROM clause when using an INNER JOIN on an UPDATE:
UPDATE WW_B
SET WW_B.WWtype_ID = 1
FROM WW_B
INNER JOIN New_data
on WW_B.StdNr = New_data.StdNr
You are missing the FROM clause
UPDATE WW_B SET WW_B.WWtype_ID=1 FROM <your table> INNER JOIN New_data on WW_B.StdNr = New_data.StdNr

Trouble with parenthesis in MS Access for SQL Inner Joins

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;

multipart Identifier cound not be bound

I reviewed several of the post on this subject and haven't been able to see resolve this error. I have linked server that I'm trying to included in my WHERE clause in SQL 2008. I was able to successfully execute the query with out the WHERE clause. I'm sure that I overlooked something just need some advice. The message for the multipart identifier is on this line in the WHERE clause.
LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME.SocialSecurityNumber
SELECT dbo.VP_PUNCHORIGIN.PERSONNUM, EXPORT_DIRECTTIME_1.SocialSecurityNumber, dbo.VP_PUNCHORIGIN.PERSONFULLNAME
FROM dbo.VP_PUNCHORIGIN INNER JOIN
LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1 ON
dbo.VP_PUNCHORIGIN.PERSONNUM = EXPORT_DIRECTTIME_1.SocialSecurityNumber
WHERE (dbo.VP_PUNCHORIGIN.PERSONNUM NOT IN
(SELECT LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME.SocialSecurityNumber AS Expr1
FROM LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1))
You are using an alias in the WHERE clause subquery but don't reference it:
SELECT p.PERSONNUM,
EXPORT_DIRECTTIME_1.SocialSecurityNumber,
p.PERSONFULLNAME
FROM dbo.VP_PUNCHORIGIN p
INNER JOIN LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1
ON p.PERSONNUM = EXPORT_DIRECTTIME_1.SocialSecurityNumber
WHERE p.PERSONNUM
NOT IN (SELECT EXPORT_DIRECTTIME_2.SocialSecurityNumber
FROM LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_2)