How to implement group by and having

Posts   
 
    
like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 28-Apr-2009 13:04:31   

I realise that the answers are probably in the documentation but I've be staring at it for hours and am still really struggling to implement this relatively simple sql via llblgenpro. If someone could explain the steps to implement this I would be very grateful:

SELECT TripStopAllActivityFullV.Name
FROM TripStopAllActivityFullV
WHERE (((TripStopAllActivityFullV.Arrive)<Now()))
GROUP BY TripStopAllActivityFullV.Name
HAVING (((Sum(TripStopAllActivityFullV.Activity_Type_Id))=1));

I have a TripStopAllActivityFullVCollection containing TripStopAllActivityFullVEntities. Do I need a TypedList/TypedView?

2 class, self serve, vb 2008, llblgen v2.6, access db.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-Apr-2009 21:11:59   

Have you read the documentation about using Having and Group By in self servicing here ?

If you have, please can you post the code as you have got it so far, and well have a stab at getting it to where you need to.

Matt

like2175
User
Posts: 83
Joined: 27-Mar-2006
# Posted on: 29-Apr-2009 08:20:38   

Thanks MTrinder. I've got is sorted now!

Dim fields As New ResultsetFields(2)
        fields.DefineField(TripStopPersonActivityFields.PersonId, 0)
        fields.DefineField(TripStopPersonActivityFields.PersonActivityTypeId, 1, "OnOffBoard", AggregateFunction.Sum)
        Dim relations As IRelationCollection = New RelationCollection()
        relations.Add(TripStopPersonActivityEntity.Relations.TripStopEntityUsingTripStopId)

        Dim groupByClause As IGroupByCollection = New GroupByCollection()
        groupByClause.Add(fields(0))

        groupByClause.HavingClause = New PredicateExpression( _
         fields(1).SetExpression(TripStopPersonActivityFields.PersonActivityTypeId * 1).SetAggregateFunction(AggregateFunction.Sum) = 1)

        Dim filter As IPredicateExpression = ((TripStopFields.LocationId = _tripStop.LocationId) And _
              (TripStopFields.StatusId <> StatusEntity.Deleted) And _
              (TripStopFields.Etd <= _tripStop.Etd))

        Dim dynamicList As New DataTable()
        Dim dao As New TypedListDAO()
        dao.GetMultiAsDataTable(fields, dynamicList, 0, Nothing, filter, relations, True, groupByClause, Nothing, 0, 0)

        lbPersonId.DataSource = dynamicList
        lbPersonId.DisplayMember = "PersonId"
        lbPersonId.ValueMember = "PersonId"

Which produces:

Query: SELECT [Trip_Stop_Person_Activity].[Person_ID] AS [PersonId], SUM([Trip_Stop_Person_Activity].[Person_Activity_type_ID] * @LLBLEP1) AS [OnOffBoard] FROM ( [Trip_Stop]  INNER JOIN [Trip_Stop_Person_Activity]  ON  [Trip_Stop].[Trip_Stop_ID]=[Trip_Stop_Person_Activity].[Trip_Stop_ID]) WHERE ( ( ( [Trip_Stop].[Location_ID] = @LocationId2 AND [Trip_Stop].[Status_ID] <> @StatusId3) AND [Trip_Stop].[ETD] <= @Etd4)) GROUP BY [Trip_Stop_Person_Activity].[Person_ID] HAVING ( SUM([Trip_Stop_Person_Activity].[Person_Activity_type_ID] * @LLBLEP6) = @PersonActivityTypeId5)
    Parameter: @LLBLEP1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
    Parameter: @LocationId2 : String. Length: 20. Precision: 0. Scale: 0. Direction: Input. Value: "1-2".
    Parameter: @StatusId3 : String. Length: 20. Precision: 0. Scale: 0. Direction: Input. Value: "Deleted".
    Parameter: @Etd4 : DateTime. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 16/05/2009 10:05:00.
    Parameter: @LLBLEP6 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.
    Parameter: @PersonActivityTypeId5 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 1.

I had to enter

TripStopPersonActivityFields.PersonActivityTypeId * 1

rather than just

TripStopPersonActivityFields.PersonActivityTypeId

to avoid "Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IExpression'."

I wonder if there is a neater way around this?