| 
	
 | 
 Posted by Hugo Kornelis on 01/12/06 00:15 
On 11 Jan 2006 13:14:43 -0800, 2redline@gmail.com wrote: 
 
>I am trying to write a query in access that will pull results that are 
>in the database in a text field and convert to where I can get a 
>average including decimals.  Any Ideas? 
> 
> 
>-----------------------Start SQL-------------------------------------- 
>SELECT AVG(CAST(dbo_sur_response_answer.answer_text AS int)) AS 
>avg_correct 
>FROM (dbo_sur_response_answer 
>INNER JOIN dbo_sur_subitem 
>ON dbo_sur_response_answer.subitem_id = dbo_sur_subitem.subitem_id) 
>INNER JOIN dbo_sur_response 
>ON dbo_sur_response_answer.response_id = dbo_sur_response.response_id 
>WHERE (((dbo_sur_response.completed_yn)="Y") AND 
>((dbo_sur_subitem.subitem_id)=478)); 
>-----------------------End SQL-------------------------------------- 
 
Hi 2redline, 
 
I doon't know much about Access, but the following should work in SQL 
Server. (I assume that using SQL Server is an option, since you 
crossposted this to a SQL Server group). 
 
SELECT     AVG(CAST(ra.answer_text AS decimal(5,2))) AS avg_correct 
FROM       dbo.sur_response_answer AS ra 
INNER JOIN dbo.sur_subitem         AS si 
      ON   si.subitem_id            = ra.subitem_id 
INNER JOIN dbo.sur_response        AS r 
      ON   r.response_id            = ra.response_id 
WHERE      r.completed_yn           = 'Y' 
AND        sit.subitem_id           = 478; 
 
I introduced aliasses and changed spacing to make the query more 
readable, changed the Access double quotes to standard SQL single quotes 
and removed all the unneeded parenthese. The only actual change I made 
was CASTing to decimal(5,2) instead of CASTint to int. The average of a 
bunch of integers will be integer as well; if you want a result with 
decimals, you'll have to use a datatype with decimals (such as 
decimal(5,2), which specifies 3 digits to the left and 2 to the right of 
the decimal point). 
 
 
--  
Hugo Kornelis, SQL Server MVP
 
  
Navigation:
[Reply to this message] 
 |