The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

GROUP BY with HAVING?

Last post 11-07-2008, 2:49 AM by David.Parsons. 7 replies.
Sort Posts: Previous Next
  •  06-18-2008, 2:08 PM 9876

    GROUP BY with HAVING?

    I'm able to use the Dynamic Query API to build a SELECT statement with a GROUP BY.  But, is there a way to extend that query to add a HAVING clause?

    For example, using Northwind, I would like to use the Dynamic Query API to generate a SQL query that looks like the following SELECT statement:

    Code:
    SELECT c.CustomerID
    FROM Customers c
    LEFT JOIN Orders o ON  c.CustomerID = o.CustomerID
    GROUP BY c.CustomerID
    HAVING COUNT(*) >= @iOrderCnt

    I can easily accomplish everything but the HAVING clause by using the following Dynamic Query in VB.NET:

     

    Code:
    Dim c As New CustomersQuery("c")
    Dim o As New OrdersQuery("o")
    c.Select(c.CustomerID)
    c.LeftJoin(o).On(o.CustomerID = c.CustomerID)
    c.GroupBy(c.CustomerID)
    

    Is there anyway to get the HAVING clause implemented using the Dynamic Query API?

     

     

  •  06-18-2008, 2:20 PM 9877 in reply to 9876

    Re: GROUP BY with HAVING?

    http://www.entityspaces.net/blog/2008/03/03/EntitySpaces+2008+Dynamic+SubQuery+Showcase.aspx

    Look here. I suspected you will find your answer here.

  •  06-19-2008, 8:26 AM 9891 in reply to 9877

    Re: GROUP BY with HAVING?

    Well, I suppose in many instances one could "rewrite" this sort of query to use Sub Queries.  But, it seems to me that a basic HAVING clause should be supported natively in the Dynamic Query API (or via some sort of "workaround" that the API enables).  I still do not see a way to do the comparison in a WHERE clause referencing an aggregate count from the inner/sub query.

    I still do not see a way to accomplish the SQL below using the Dynamic Query API:

     

    Code:
    SELECT c2.[CustomerID] AS 'CustomerID',
    c2.[CompanyName] AS 'CompanyName',
    c2.[ContactName] AS 'ContactName',
    c2.[ContactTitle] AS 'ContactTitle',
    c2.[Address] AS 'Address',
    c2.[City] AS 'City',
    c2.[Region] AS 'Region',
    c2.[PostalCode] AS 'PostalCode',
    c2.[Country] AS 'Country',
    c2.[Phone] AS 'Phone'
    FROM (SELECT c.[CustomerID],
    COUNT(o.[OrderID]) AS 'OrdCnt'
    FROM [Customers] c
    INNER JOIN [Orders] o ON o.[CustomerID] = c.[CustomerID]
    GROUP BY c.[CustomerID]
    ) sub
    INNER JOIN Customers c2 ON sub.CustomerID = c2.CustomerID
    WHERE sub.OrdCnt = <parm value>
     

    I wonder if adding the "< arbitrary sql goes here >" functionality mentioned in the post below and on the Roadmap will be done in a way as to enable us to add a HAVING?

    http://community.entityspaces.net/forums/thread/6899.aspx

     

  •  06-20-2008, 5:10 AM 9909 in reply to 9891

    Re: GROUP BY with HAVING?

    Yes, Having is important, it didn't make this release but will make a follow on maintenance release. I cannot give you a date as of yet on when that will be however.

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  08-27-2008, 4:19 PM 10980 in reply to 9909

    Re: GROUP BY with HAVING?

    Hey Mike, are we any closer to seeing a Having() method?

    ES 2008.1.1110.0
    VS 2008 Pro SP1 on Vista Ultimate x64
    SQL Server 2000
  •  10-02-2008, 12:57 AM 11747 in reply to 10980

    Re: GROUP BY with HAVING?

    Anyone know what the status is of the Group By and Having functionality? If it's not going to be a feature what would be a work around with ES?

     

     

  •  11-06-2008, 4:25 PM 12284 in reply to 11747

    Re: GROUP BY with HAVING?

    *bump*
    ES 2008.1.1110.0
    VS 2008 Pro SP1 on Vista Ultimate x64
    SQL Server 2000
  •  11-07-2008, 2:49 AM 12291 in reply to 12284

    Re: GROUP BY with HAVING?

    That feature request is still on our enhancement tracking list, but there is no scheduled release date.

    The current work-arounds are the SubQuery suggestion above, or a Custom Load.


    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML