Skip to content
Advertisement

How to make sum of rows value until sum value is 1.0 in SQL Server

I am stuck with this SQL Server query; here is my set of records:

I want a query which will give me sum of value until it reach 1.0 and greater then continue same with next rows, like

Please help me with this query; I tried with partitions and recursive CTE, but nothing worked so far.

Advertisement

Answer

SQL tables represent unordered sets. Your results assume a particular ordering and there is no column for the ordering. The following assumes that such a column exists. It is just called id.

This type of problem requires a recursive CTE (or some other iterative processing), as far as I know. Here is one method:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement