Sum and mean sql table in java

Asked

Viewed 294 times

1

I’m trying trouble with something I think until simple, but I don’t know which command I should run to do this.

All right, here we go: I have a table (let’s call MOVIES) ...and this table has 3 columns.

What I need:

I need to sum all the values in column 2 of this MOVIES table and divide by the total number of values. That is, let’s assume that my second column has 3 values: 2, 3 and 4.

I need to add these 3 values and then divide by 3.

Thus: (2+3+4)/3.

I even know how to take the sum with the cursor in Count. But, I do not know how to recover the sum (use SUM) in the form of a value that I may be dividing by the total number.

Breaking would need it to only add up the items in column 2, whose value in column 3 is 0. But, I can see later, if you help me recover the sum of the columns so I can divide by the total would be helpful.

  • João Paulo, go to Ajuda and take the Tour, see how you should ask here at SOPT, then edit your question and add part of your code and table (if you have), so it will be easier to help you!

1 answer

0

It is quite simple just to carry out a projection, in code already with the criterion of the third column being greater than 0 is like this:

select 
  SUM(nome_coluna2) / COUNT(*)
from 
  Filmes
where
  nome_coluna3 = 0;

Any doubt can comment on the answer I clarify.

  • I also created a sql fiddle to illustrate the example better, as follows: http://sqlfiddle.com/#! 9/1d02bd/2

  • Sorry. I didn’t know that’s how this forum works. Well, more anyway .. I’m waiting for your Dit. I need a way to put this in java, IE, a way to recover this value. If I just type it there goes wrong.

  • It would not be easier and more practical to use the avg function directly?

  • Select avg(column) from table , simple media discards null values

Browser other questions tagged

You are not signed in. Login or sign up in order to post.