Reputation: 695
I have a table like this in MYSQL.
create table Employee(
id tinyint,
name char(36),
is_permanent tinyint,
company_name char(36),
designation Char(36),
primary key (id)
);
and I want to write a SELECT query on this table with Quill SQL.
select count(1), sum((case when (`designation` = 'software') then 1 else 0 end)) from employee group by company_name;
I wrote a model and
case class Employee(id: Int,
name: String,
is_permanent: Boolean,
company_name: String,
designation: Designation)
trait Designation {
val value: String
}
case object SoftwareEngineer extends Designation {
override val value: String = "software"
}
case object MechanicalEngineer extends Designation {
override val value: String = "mechanical"
}
My query using quill is
ctx.run {
query[Employee].groupBy(_.company_name).map {
case (compName, employeeTable) =>
(employeeTable.size,
employeeTable.filter(_.designation == lift(SoftwareEngineer)).size,
compName)
}
}
where ctx
is the quill context.
However the compiler shows an error that
exception during macro expansion:
[error] java.lang.IllegalStateException: Invalid group by aggregation: 'x48.filter(x49 =>
x49.designation == ?).size'
Is the way of calculating filtered count in the group by clause correct ? If no, Is there other way to do it ?
Upvotes: 0
Views: 456
Reputation: 695
The way to find out conditional count for a group by clause is NOT
employeeTable.filter(_.designation == lift(SoftwareEngineer)).size,
but
employeeTable.map { i =>
if (i.`designation` == lift(SoftwareEngineer)) 1
else 0
}.sum
Of course with proper encoder defined for designation
Upvotes: 0