ISqlHavingBuilder.Having(string left, ISqlBuilder sqlBuilder) Method
.NET Standard 2.x
Creates a HAVING
clause and adds a search condition to the HAVING
clause. You can specify a SQL expression on the left of the operator and a SQL subquery on the right side. The operator is '='.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
ISqlHavingAndOr Having(string left, ISqlBuilder sqlBuilder);
Parameters
left
System.String
A SQL expression on the left of the operator.
sqlBuilder
SnapObjects.Data.ISqlBuilder
An ISqlBuilder
object which represents a SQL subquery on the right of the operator.
Returns
SnapObjects.Data.ISqlHavingAndOr
Returns the ISqlHavingAndOr
object which can be used to add more search conditions to the current HAVING
clause.
Examples
The following code example demonstrates how to use the Having method to specify the HAVING condition for groups.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.ISqlHavingBuilderExamples
{
public class HavingExample
{
private readonly SchoolContext _context;
public HavingExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example5()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Sets a query to get the smallest Course ID.
var sqlBuilder = new SqlQueryBuilder();
sqlBuilder.SelectRaw("min(CourseID)")
.From("Course");
// Groups "StudentGrade" table by StudentID and CourseID, and gets the sum of Grade of each group.
// CourseID is equal to the return value of the subquery.
sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.Having("CourseID", sqlBuilder);
// Converts to raw SQL for the database corresponding to the data context.
string sql = sqlQueryBuilder.ToSqlString(_context);
Console.WriteLine(sql);
/*This code example produces the following output:
SELECT
[StudentID],
sum(Grade)
FROM [StudentGrade]
GROUP BY
[StudentID],
[CourseID]
HAVING ( [CourseID] = (SELECT
MIN([CourseID])
FROM [Course]))
*/
}
}
}
Applies to
.NET Standard
2.x