Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all, I have an Access 2003 database . That database contains around 60,000 rows of data dealing with their vendor data such as total TargetWeight, ActualWeight , etc... I'm collecting data from this database to populate mt gridview in c#which I can later develope a Summary report of sorts showing average TargetWeight, AVG(ActualWeight)and other info grouped by the ID . Easy enough right?
Query works fine in Access ,but in c# I keep getting error like am using a reserved word, And in fact I have tried all the Aggregate Function in my query from c# to Access DB ,but failed up to this point, help me if am missing something thanks.

command.CommandText = " SELECT t1.IngredientLotId1, t1.IngredientName, t1.ActualWeight, t1.TargetWeight, ROUND((t1.ActualWeight- t1.TargetWeight ),0)AS VARIANCE,ROUND( (0.04* t1.TargetWeight ),0)AS TOLERANCE,ROUND(([t1].[TargetWeight]),0),AVG(t1.TargetWeight) AS AvgTargetWeight FROM CompletedIngredients As t1 INNER JOIN CompletedFormulas As t2 ON t1.RecordNumber=t2.RecordNumber ORDER BY t2.StartTime ASC;
Posted
Updated 2-Apr-15 0:26am
v2
Comments
Maciej Los 2-Apr-15 6:57am    
Check the list of reserved words: http://support.microsoft.com/en-us/kb/286335
Note, when you're using aggregate function (such as AVG), you need to group data: http://www.blueclaw-db.com/accessquerysql/sql_aggregate_function.htm
Ekona_Pikin 2-Apr-15 10:51am    
Hey Marciej,just so you know It did worked now when ever I used Group By clause So thanks again:))
Maciej Los 2-Apr-15 11:13am    
So, i'll post it as an answer. Agree?

There is a comma that should not be here just before your FROM statement.
 
Share this answer
 
Comments
Ekona_Pikin 2-Apr-15 6:22am    
Hi Phil, I got that comma though that was a typo from me ,In my app is without the comma you pointed out ,I still get this bisare error"The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect."
As i mentioned in comment to the question, one possible reason that your query does not work is that you forgot to add GROUP BY clause together with AVG() function. Finall query should look like:
SQL
SELECT t1.IngredientLotId1, t1.IngredientName, t1.ActualWeight, t1.TargetWeight, ROUND((t1.ActualWeight- t1.TargetWeight ),0) AS VARIANCE,
    ROUND( (0.04* t1.TargetWeight ),0) AS TOLERANCE, ROUND(([t1].[TargetWeight]),0) AS TargetWeight, AVG(t1.TargetWeight) AS AvgTargetWeight
FROM CompletedIngredients As t1 INNER JOIN CompletedFormulas As t2 ON t1.RecordNumber=t2.RecordNumber
GROUP BY t1.IngredientLotId1, t1.IngredientName, t1.ActualWeight
-- ORDER  BY t2.StartTime ASC;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900