Add range of values under a column using rowid

oracle

How do i add range of values in a column based on row id?
For example: I have to add all the values under column "time" where rowid>=1 and rowid<=5.

Additionally is it possible to divide the then obtained sum by 3600?

Best Answer

Technically, you could use Oracle's SUM function (good explanation here: http://www.techonthenet.com/oracle/functions/sum.php). Your code would look something like this:

select sum(time)/3600
  from table_name
 where rownum >= 1 and rownum <=5;

However, without knowing more about the table, I don't know if the TIME column is simply an integer, or is a date/timestamp field. If it is date/timestamp, SUM won't really do what you need it to.

One other word of caution, using rownum doesn't always return the same results between runs. Please take a look at http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html for a much more succinct explanation than I can provide.