I did check the link as well as the code examples you provided for the Northwind database but could still use some help.
Basically, I am trying to get the current or last set of records by date.
Here is a query that gets the last customers orders for a particular employee using the Northwind Orders table:
'SELECT CustomerID, MAX(OrderDate) AS LastOrderDate, EmployeeID
'FROM dbo.Orders
'GROUP BY CustomerID, EmployeeID
'HAVING (EmployeeID = 1) AND (MAX(OrderDate) < CONVERT(DATETIME, '1997-01-01 00:00:00', 102))
This returns 6 records.
Here is my attempt at accomplishing the same with the Query Factory:
<TestMethod()>
Public Sub GetLastOrderDatePerCustomerUsingJoinWithGroupedSet()
Dim QF As QueryFactory = New QueryFactory()
Dim q = QF.Order.From(
QueryTarget.InnerJoin(QF.Create("q1"
).Select(OrderFields.OrderDate.Max().As("LastOrderDate"),
OrderFields.CustomerId,
OrderFields.OrderId.Min
).Where((OrderFields.EmployeeId = 1).And(OrderFields.OrderDate < New Date(1997, 1, 1))
).GroupBy(OrderFields.CustomerId)
).On(OrderFields.OrderId = QF.Field("OrderId").Source("q1")))
Dim orders = New OrderCollection()
orders.GetMulti(q)
Assert.AreEqual(6, orders.Count)
End Sub
This returns 22 records.
Here is my attempt at using a query without a join:
<TestMethod()>
Public Sub GetLastOrderDatePerCustomerUsingGroupByWithHaving()
Dim QF As QueryFactory = New QueryFactory()
Dim q = QF.Order.Select(OrderFields.OrderDate.Max().As("LastOrderDate")
).GroupBy(OrderFields.CustomerId
).Having(OrderFields.EmployeeId = 1)
Dim orders = New OrderCollection()
orders.GetMulti(q)
Assert.AreEqual(65, orders.Count)
End Sub
This generates a dynamic query which is not a valid argument for GetMulti.
Clearly, I am missing the obvious.
Thanks