SubSonic Forums
All Your Database Are Belong To Us

SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

Latest post 07-07-2008 9:39 PM by jamesewelch. 4 replies.
  • 07-07-2008 2:13 PM

    SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

    I'm using SqlQuery and the below syntax (field and table names changed)

    Table with 4 columns: myGuidField, myDateField, myNumberField, myStringField

     SqlQuery qry = new Select(
        Aggregate.Avg(myTable.Columns.myNumberField),
        Aggregate.GroupBy(myTable.Columns.myStringField))
        .From(myTable.Schema)
        .Where(myTable.Columns.myDateField)
        .IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-10));

    The problem is that it generates this SQL:

    SELECT AVG(myNumberField), myStringField
    FROM [dbo].[myTable]
    GROUP BY myStringField
    HAVING [dbo].[myTable].[myDateField] >=@Date0

    This generates an error because "Column 'dbo.myTable.myDateField' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause."

    If SqlQuery generated the SQL code as a WHERE clause instead of HAVING clause then it would work. For example, this works:

    SELECT avg(myNumberField), myStringField
    FROM myTable
    WHERE myDateField >= '6/29/2008'
    GROUP BY myStringField

     

    Is there a work around for this? I just need the SqlQuery to generate my Where expression using WHERE instead of HAVING.

     

    thanks,

     

  • 07-07-2008 8:31 PM In reply to

    Re: SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

     

    What build are you using ?

    The latest build creates the correct SQL for this test.

     

     

     

     

     

     

     

         [Test]
            public void Exec_AggregateWithWhereNotHaving()
            {
                DateTime dTestDate = DateTime.MinValue;
                int records = new
                Select(Aggregate.GroupBy("ProductID"), Aggregate.Avg("UnitPrice"))
                .From("Order Details")
                .Where("Quantity").IsEqualTo(120)
                .GetRecordCount();

                Assert.AreEqual(7, records);
            }

     

  • 07-07-2008 8:47 PM In reply to

    Re: SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

    I'm using 2.1.0.0 RC1.

  • 07-07-2008 9:20 PM In reply to

    Re: SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

     

    Several issues with regards aggregates were addressed in Revision 471.

    Latest builds can be downloaded using client such as Tortoise from http://subsonicproject.googlecode.com/svn/trunk

  • 07-07-2008 9:39 PM In reply to

    Re: SqlQuery error - Invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause

    Thanks.

Page 1 of 1 (5 items) | RSS