2

How do I map a count() value from ResultRow using QueryAlias in Exposed
Or am I doing something completely wrong?

val countAlias = EventTable.join(AttendeeEventTable, JoinType.INNER)
    .slice(EventTable.id, EventTable.id.count())
    .selectAll()
    .groupBy(EventTable.id)
    .alias("countAlias")

EventTable
    .join(countAlias, JoinType.LEFT, EventTable.id, countAlias[EventTable.id])
    .selectAll()
    .map {
         Event(
               id = it[EventTable.id].value,
               name = it[EventTable.name],
               countOfAttendeees = it[countAlias[EventTable.id.count()]],  //Not working. How to get the count() from ResultRow
              )
         }

oletjens
  • 31
  • 4

1 Answers1

3

Just define a count "column" before the query. Here is an example:

        val count = FooTable.status.count().alias("count")

        FooTable
            .slice(FooTable.status, count)
            .selectAll()
            .groupBy(FooTable.status)
            .map {
                FooCount(
                    it[FooTable.status],
                    it[count]
                )
            }

As you see, I have create a value count and then I am using it in the slice selection and later in the row-mapping.

igr
  • 10,199
  • 13
  • 65
  • 111