G
G
GLaDosSystem2019-06-01 14:40:08
PostgreSQL
GLaDosSystem, 2019-06-01 14:40:08

How to divide a SQL table into N parts and perform calculations on each?

There is a table like this:

timestamp           | a   | b
-------------------------------
01.02.2018 10:20:05 | 100 | 200
01.02.2018 10:20:10 | 150 | 150
01.02.2018 10:20:15 | 100 | 300

...... ещё очень много строчек .......

Can I divide this whole table into n parts with approximately the same number of rows, and then perform a certain mathematical operation on these parts (like avg ())?
Concerning division of the table - here this task is well described.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TheRonCronix, 2019-06-01
@TheRonCronix

I give an idea (did not check the syntax):
select mod(rn, N) part_number,
t1.*
from
(
select t.*,
row_number() over() rn
from table as t
) as t1
N - number of parts, table - your table.

D
d-stream, 2019-06-01
@d-stream

Some kind of vague desire from the series to calculate the sum of random lines very accurately)
In general - window functions - there is just over partition by - it remains only to set how to partition and what to do in the over

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question