June 27, 2008 at 2:58 pm
how do you convert integers to decimals or floats in order to calculate percentages.
I tried this:
SELECT a.ClassId, s.race,
SUM(case when a.Status='P' then 1 else 0 end) AS Present, SUM(case when a.Status='A' then 1 else 0 end) AS Absent, CONVERT(DECIMAL, Absent/(Present + Absent)) AS PercentAbsent
But I got errors. Is my convert expression correct?
June 27, 2008 at 3:19 pm
The error you are getting is because you are trying to reference the alias instead of the data itself.
For example:
DECLARE @a int
DECLARE @b-2 int
SET @a = 1
SET @b-2 = 3
SELECT CONVERT(DECIMAL(12,3), @a)/@b AS PercentAbsent
will give you the percentage.
However, I think you should look at you query and group the data because I think you want something like
class
, race
, (SELECT COUNT(id) FROM table WHERE absent) / (SELECT COUNT(id) FROM table)
but that is speculation:)
June 27, 2008 at 3:28 pm
Please supply more data. Refer to link in my signature. Supply complete error statement including error number, state and text and then maybe we can help
June 27, 2008 at 6:30 pm
Mark (6/27/2008)
But I got errors.
Can't read your mind, Mark... please post the exact text from the errors...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply