Path: redb/redb.Examples/Examples/E133_WindowRunningSum.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/Examples/E133_WindowRunningSum.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Core.Query.Aggregation;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Window function SUM() OVER for running totals.
/// Calculates cumulative salary within each department.
///
[ExampleMeta("E133", "Window - Running Sum", "Analytics",
ExampleTier.Free, 133, "Window", "Sum", "Running", "Cumulative", RelatedApis = ["IRedbQueryable.WithWindow", "Win.Sum"])]
public class E133_WindowRunningSum : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
var windowQuery = redb.Query()
.Take(50)
.WithWindow(w => w
.PartitionBy(x => x.Department)
.OrderBy(x => x.HireDate));
// Uncomment to see generated SQL:
// var sql = await windowQuery.ToSqlStringAsync(x => new { x.Props.Salary, Total = Win.Sum(x.Props.Salary) });
// Console.WriteLine(sql);
var running = await windowQuery.SelectAsync(x => new
{
Name = x.Props.FirstName,
Department = x.Props.Department,
Salary = x.Props.Salary,
RunningTotal = Win.Sum(x.Props.Salary)
});
sw.Stop();
var sample = running.FirstOrDefault();
return Ok("E133", "Window - Running Sum", ExampleTier.Free, sw.ElapsedMilliseconds, running.Count,
[$"SUM(Salary) OVER (PARTITION BY Dept ORDER BY HireDate)", $"Sample: {sample?.Name ?? "N/A"}, Running: {sample?.RunningTotal ?? 0:N0}"]);
}
}