AnishaJain AnishaJain - 1 year ago 50
SQL Question

How to sum 8 rows at a time in a select statement

I have a select statement that gives a set of rows whose count is always a multiple of 8.

What I want to do is to find the sum of the first 8 rows, the second 8 rows and so on. Is there a way to do this

Answer Source
select a from test;

select r/8, SUM(a) from (select ROW_NUMBER() over (order by a)-1 as r,a from test) tab group by r/8;

enter image description here