AnishaJain AnishaJain - 6 months ago 14
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
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