querydsl group by sum example

Example 1: linq sum group by

var result = source
    .GroupBy(x => x.Currency)
    .Select(g => new {
        Currency = g.Key,
        Total = g.Sum(x => x.FeesCustom > 0 ? x.FeesCustom : x.FeesNormal)
    });

Example 2: sql group by sum

// Use SQL Group By clause after the Where clause
// Any column in the select list must either be in the Group By clause 
// or part of an agregation statement, like the Sum() statement
// Multiple columns can be included in the Group By Clause seperated by a comma
// Multiple columns stipulate the order tree. Order by 
// first column, then second ... etc

Select LastName, FirstName, Sum(LeaveDays), Max(age)
From EmployeeProjects
Where StartDate > '2020-01-01'
Group By LastName, FirstName

Tags:

Sql Example