【问题标题】:exception during macro expansion: [error] java.lang.IllegalStateException: Invalid group by aggregation:宏扩展期间的异常:[错误] java.lang.IllegalStateException:聚合分组无效:
【发布时间】:2020-01-13 06:43:28
【问题描述】:

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

我使用 quill 的查询是

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

ctx 是 quill 上下文。

但是编译器显示一个错误

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

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

【问题讨论】:

    标签: mysql scala group-by case quill.io


    【解决方案1】:

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

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

    但是

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

    当然要为designation定义适当的编码器

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2013-12-08
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多