Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

SQL divide query

Dear expert i need help on this sql query:

select sum (money)
from dayground
where Daycode = 1 and Typecod = 4 and date = '20790606'
and money >= 25000

divide by

select count (memberno)
from dayground
where Daycode = 1 and Typcod = 4 and date = '20790606'
and money >= 25000

Anyway to do it?
Thx
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi
WeTi

ASKER

Very fast and correct answer. thanks man
What database?

Am I missing the reason you cannot do:
select sum (money)/count (memberno)
from dayground
where Daycode = 1 and Typecod = 4 and date = '20790606'
and money >= 25000

Open in new window


You will need to worry when the count = 0.  What do you want to happen then?  It would likely be a CASE statement.
Avatar of WeTi

ASKER

One more thing, how do you round the result to 0 decimal?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can surely cast:
select cast (
(
select sum (money)
from dayground
where Daycode = 1 and Typecod = 4 and date = '20790606'
and money >= 25000
)
/
(
select count (memberno)
from dayground
where Daycode = 1 and Typcod = 4 and date = '20790606'
and money >= 25000
)
as int)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

Because im a beginner and didn't know how to use cast fully