Is it possible to multiply the result of a count function by another column
840290Apr 26 2011 — edited Apr 26 2011Hello,
I need to generate an invoice for the amount of nursery sessions a given child has attended. I can get the amount of sessions using this query:
select COUNT(s.session_id), cr.child_id
from sessions s, child_register cr
where s.session_id = cr.session_id
and cr.child_id = 'C110001'
group by cr.child_id;
Which outputs:
2 C110001
I can't figure out how to multiply the result of COUNT(s.session_id) by a row in the session table called srate (which is a number, 20)
Here are my attempts:
select COUNT(s.session_id) * s.rate, cr.child_id
from sessions s, child_register cr
where s.session_id = cr.session_id
and cr.child_id = 'C110001'
group by cr.child_id;
I get an error 'Not a group by expression which makes sense
Then I tried:
select COUNT(s.session_id) * s.rate AS total, cr.child_id
from sessions s, child_register cr
where s.session_id = cr.session_id
and cr.child_id = 'C110001'
group by total;
Error 'total' not a valid identifier, Wrapping the COUNT(s.session_id) * s.rate in parentheses gives me the same error.
I'm sure it's something blatantly obvious :(
Any help very much appreciated as always,
Mike