tags:

views:

3969

answers:

1

I have a problem with an SQL query on Postgresql. This select clause is an example from a lecture on databases:

1 select t.CourseNr, t.StudentsPerCourse, g.StudentCount, 
2        t.StudentsPerCourse/g.StudentCount as Marketshare
3 from (select CourseNr, count(*) as StudentsPerCourse
4       from taking
5       group by CourseNr) t,
6      (select count(*) as StudentCount
7       from Students) g;

The problem is the Marketshare column in line 2. Both StudentsPerCourse and StudentCount are of type integer.

When using this on my Postgresql database, the Marketshare column is evaluated as an int type, while i would need a float/numeric here. I didn't find any way to specify the data type by searching the Postgresql Documentation on SELECT clauses nor by googling. Is there a (preferably standard SQL) way to specify the column type or am I missing something here?

+6  A: 

CAST() one or both of the source columns as a decimal/float/real/double/etc type.

Joel Coehoorn
Yep, that helped :) Thanks!
VolkA
Alternate casting syntax is "SELECT mycolumn::real FROM mytable;" but I think the CAST() function is more portable - you should probably stick to that.
Neall
I like that shorthand: I wish it was supported better among different vendors.
Joel Coehoorn