question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Joining a last activity record

See original GitHub issue

I’ve following two entities where I’m trying to get the latest activity for an employee:

interface Activity : Entity<Activity> {
    val id: Long
    val desc: String
    val employee: Employee
    val createdAt: LocalDateTime?
    companion object : Entity.Factory<Activity>()
}

interface Employee : Entity<Employee> {
    var id: Long
    var name: String
    
    val latestActivity: Activity?
        get() {
            var activity = this["latestActivity"]
            if (activity == null) {
                activity = Activities.asSequenceWithoutReferences().sortedByDescending { it.createdAt }
                    .filter { it.employeeId eq id }.firstOrNull()
                this["latestActivity"] = activity
            }
            return activity as? Activity
        }
    companion object : Entity.Factory<Employee>()
}

This all works great but unfortunately for each employee getting the latestActivity is an extra query. What’s the best way to fetch Employees and have it returned with latestActivity joined?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vincentlauvlwjcommented, Sep 19, 2019

I also tried to use a native SQL. In this way, only one query is executed:

val employees = useConnection { conn ->
    val sql = """
        select emp.id as emp_id, emp.name as emp_name, act.id as act_id, act.desc as act_desc, act.created_at as act_time
        from employee emp left join (
            select a.id, a.employee_id, a.desc, a.created_at
            from activity a
            where a.id = (
                select max(b.id)
                from activity b
                where b.employee_id = a.employee_id
            )
        ) act on emp.id = act.employee_id;
    """.trimIndent()

    conn.prepareStatement(sql).use { statement ->
        statement.executeQuery().use { rs ->
            rs.iterable().map { row ->
                Employee {
                    id = row.getLong("emp_id")
                    name = row.getString("emp_name")
                    latestActivity = Activity {
                        id = row.getLong("act_id")
                        desc = row.getString("act_desc")
                        createdAt = row.getTimestamp("act_time")?.toLocalDateTime()
                    }
                }
            }
        }
    }
}
0reactions
ashokgelalcommented, Sep 20, 2019

Great! I would be very happy to test it out on a real database once ready. Any idea about when it might be available?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL join: selecting the last records in a one-to-many relationship
Go to the transaction table with multiple records for the same client . Select records of clientID and the latestDate of client's activity...
Read more >
Creating a Last Activity Date Formula Field
I'm looking to use Last Activity Date for a report that I am creating for my Sales team, but it does not appear...
Read more >
Display Last Activity Date on Object Record Detail Page
The last activity date for an account record is based on all the activities that roll up to the account via the Related...
Read more >
How do I capture the LAST activity date and note?
I figured the best way to get my reports to work is to create a field named "Last PR Comment" on my custom...
Read more >
Workflow Record Last Date Activity by Team
Workflow Record Last Date Activity by Team. Hello all, Inside the company I work for, the financial team also uses Hubspot to log...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found