I’ve got a MS Access 2007 DB with records of passed and failed attempts to pass an exam.
Student_id, Course_id, passed
S001 C001 0
S001 C001 1
S002 C001 1
S003 C001 0
‘Passed’ is used as a boolean where 0 is failed an 1 passed, but is stored as a number.
I want to build a query displaying the number of attempts per student per course. This can be done by averaging the number passed. S001 has an average of 0.5, S002 of 1 and S003 of 0. This would indicate that S001 passed after 2 attempts, S002 after 1, and S003 never made it.
SELECT Student_id, Course_id, avg(passed)
FROM tbl
GROUP BY Student_id, Course_id, passed
The problem is: the average’s are all 0 or 1. My guess is that the number does not convert to a double (allowing for decimals). How do I cast the average in to a datatype allowing decimals?
I don’t see the problem. I created a test table with the same data, and when I run this SQL:
I get this result:
Student_ID Course_ID AvgOfPassed S001 C001 0.5 S002 C001 1 S003 C001 0The SQL you original posted should produce a row for every row in your original table, since you mistakenly included PASSED in the GROUP BY.
My question for you is where you are viewing the data that you think it’s producing the wrong results. It’s quite clear that with the data you’ve provided and the description of the desired results, Access is returning exactly what you asked for if you write the SQL correctly.