2 years ago

#64696

test-img

AlexeyTokar

How to get running result for balance sheet in ClickHouse

Assume I have a table that looks like this:

+---------+------------+--------+
| id      | operation  |amount  |
+---------+------------+--------+
| 1       | add        |10      |
| 2       | add        |10      |
| 1       | sub        |5       |
| 1       | sub        |5       |
| 2       | sub        |10      |
+---------+------------+--------+

I want to transform it into something like this, where every next actual records column contains all array elements from the previous row with added/removed elements from the current row based on the value in operation column:

+---------+------------+--------+------------------------+
| id      | operation  |amount  | actual records         |
+---------+------------+--------+------------------------+
| 1       | add        |10      | [(1,10)]               |
| 2       | add        |10      | [(1,10),(2,10)]        |
| 1       | sub        |5       | [(1,5),(2,10)]         |
| 1       | sub        |5       | [(2,10)]               |
| 2       | sub        |10      | []                     |
+---------+------------+--------+------------------------+

Is it possible at all with ClickHouse only? Might window functions be in help?

database

aggregation

clickhouse

0 Answers

Your Answer

Accepted video resources