kkk
kkk

Reputation: 112

DBFlow Android select most common value

I want to select most common name using dbFlow. In SQLite it would be:

SELECT `Employee`.name
FROM `Employee`
GROUP BY `Employee`.name
HAVING COUNT(*) =
  (SELECT MAX(cn) FROM
     (SELECT `Employee`.name, COUNT(*) AS cn
      FROM `Employee`
      GROUP BY `name`))

I've tryed:

        SQLite.select(Employee_Table.name)
                .from(Employee.class)
                .groupBy(Employee_Table.name)
                .having(Method.count().eq(
                        SQLite.select(
                                Method.max("cn")).from(
                                SQLite.select(Employee_Table.name, Method.count().as("cn"))
                                        .from(Employee.class)
                                        .groupBy(Employee_Table.name)
                        )
                        )
                )

But Method.max doesn't accept alias. Or should I run just a raw query?

Upvotes: 2

Views: 365

Answers (1)

Inverce
Inverce

Reputation: 1496

Method.max accepts only property You can create one with.

public static Property<Long> name(String value) {
    return new Property<Long>(null, value) {
        @Override
        public String toString() {
            return nameAlias.nameRaw();
        }
    };
}

usage:

Method.max(name("cn"))

Upvotes: 1

Related Questions