Join based on most recent record

Posts   
 
    
reprezent
User
Posts: 11
Joined: 17-Feb-2007
# Posted on: 15-Mar-2007 23:51:32   

I have 2 tables, the LoginInfo table which holds a user's login name and password and the LoginAttempt table which holds his login attempts. The two tables are linked together on the UserID field in a one-to-many relationship.

Assuming each record in the LoginAttempt table contains a DateAttempted field, how would I use LLBL to return a joined result set which included one record per user containing the Username field from the LoginInfo table and the Status field from the LoginAttempt table of just his last login attempt (based on the DateAttempted field)?

In other words, if the username "John" had 5 login attempts with the last one being successful and "Mary" had 3 login attempts with the last one being a failure, the query should return the results of only the last attempt, e.g.:

User | Status John | OK Mary | Failed

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 16-Mar-2007 05:44:23   

You can do this using entities by defining a prefetch path to LoginAttempt from LoginInfo. When defining the prefetch path you can supply a sorter and define that you only want 1 record prefetched.

Let me know if this may work or if it is important that you only return the columns that you listed in your post.

reprezent
User
Posts: 11
Joined: 17-Feb-2007
# Posted on: 16-Mar-2007 05:51:35   

Thanks for the reply, appreciate the help.

You can do this using entities by defining a prefetch path to LoginAttempt from LoginInfo. When defining the prefetch path you can supply a sorter and define that you only want 1 record prefetched.

I'm not (yet) an expert on prefetch paths, do you have a rough example of what that syntax would look like?

Let me know if this may work or if it is important that you only return the columns that you listed in your post.

It doesn't have to return only those columns, it just seems like it would be more efficient than returning tons of columns which I don't need, no?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Mar-2007 09:25:08   

Please refer to the LLBLGen Pro manual: "Using the generated code -> SelfServicing/Adapter -> Prefetch Paths"