SqlBuilder.Having(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder) Method
.NET Standard 2.x
Creates an ISqlHavingCondition
object which represents a search condition that can be used when building the HAVING clause. Specifies a SQL expression on the left of the operator and a SQL subquery on the right.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public static ISqlHavingCondition Having(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder);
Parameters
left
System.String
A SQL expression on the left of the operator.
SqlBinaryOperator
SnapObjects.Data.SqlBinaryOperator
An enumeration value of SqlBinaryOperator
, which is the operator to test the two expressions on the left and right.
sqlBuilder
SnapObjects.Data.ISqlBuilder
An ISqlBuilder
object which represents a SQL subquery on the right of the operator.
Returns
SnapObjects.Data.ISqlHavingCondition
Returns the ISqlHavingCondition
object which can be used to add more search conditions to the 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.SqlBuilderExamples
{
public class HavingExample
{
private SchoolContext _context;
public HavingExample(SchoolContext dataContext)
{
// Sets Data Context.
_context = dataContext;
}
public void Example6()
{
var sqlQueryBuilder = new SqlQueryBuilder();
// Gets the smallest Course ID, and uses it as a parameter for the HAVING condition.
var sqlBuilder = new SqlQueryBuilder();
sqlBuilder.SelectRaw("min(CourseID)")
.From("Course");
// Defines a HAVING condition.
var having = SqlBuilder
.Having("CourseID", SqlBinaryOperator.GreaterThan, sqlBuilder);
// Builds a SQL statement to retrieve StudentID and sum(Grade) from the "StudentGrade" table and
// group records by StudentID and CourseID which meet the condition: StudentID greater than the result set of the sqlBuilder subquery.
var query = sqlQueryBuilder
.Select("StudentID")
.Select("sum(Grade)")
.From("StudentGrade")
.GroupBy("StudentID, CourseID")
.Having(having);
// Converts to raw SQL for the database corresponding to the data context.
string sql = query.ToSqlString(_context);
Console.WriteLine(sql);
/*This code 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