0

我在 MYSQL 中有一个这样的表。

create table Employee(
 id tinyint,
 name char(36),
 is_permanent tinyint,
 company_name char(36),
 designation Char(36),
 primary key (id)

);

我想用 Quill SQL 在这个表上写一个 SELECT 查询。

 select count(1),  sum((case when (`designation` = 'software') then 1 else 0 end)) from employee group by company_name;

我写了一个模型

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"
  }

我使用羽毛笔的查询是

ctx.run {
  query[Employee].groupBy(_.company_name).map {
    case (compName, employeeTable) =>
      (employeeTable.size,
       employeeTable.filter(_.designation == lift(SoftwareEngineer)).size,
       compName)
  }
}

ctx羽毛笔上下文在哪里。

但是编译器显示一个错误

exception during macro expansion: 
 [error] java.lang.IllegalStateException: Invalid group by aggregation: 'x48.filter(x49 => 
 x49.designation == ?).size'

group by 子句中计算过滤计数的方式是否正确?如果没有,还有其他方法吗?

4

1 回答 1

0

找出 group by 子句的条件计数的方法不是

 employeeTable.filter(_.designation == lift(SoftwareEngineer)).size,

 employeeTable.map { i =>
          if (i.`designation` == lift(SoftwareEngineer)) 1
          else 0
        }.sum

当然,为designation

于 2020-01-14T05:06:00.970 回答