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
LVL 1
WeTiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
what about this query?

select
(
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
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
Very fast and correct answer. thanks man
0
slightwv (䄆 Netminder) Commented:
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.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

WeTiAuthor Commented:
One more thing, how do you round the result to 0 decimal?
0
slightwv (䄆 Netminder) Commented:
>>how do you round the result to 0 decimal?

Depends on the database product.  Most databases have a round function.  Check the docs?

Still curious why you couldn't do the sum and count in the same query...
0
Éric MoreauSenior .Net ConsultantCommented:
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

0
Éric MoreauSenior .Net ConsultantCommented:
a good point from NetMinder, your query would be faster by doing a single query:

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

Open in new window

0
WeTiAuthor Commented:
Because im a beginner and didn't know how to use cast fully
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.