gpt4 book ai didi

mysql - 宏扩展期间出现异常 : [error] java. lang.IllegalStateException:聚合组无效:

转载 作者:行者123 更新时间:2023-11-29 15:15:38 25 4
gpt4 key购买 nike

我在 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 是 quill 上下文。

但是编译器显示错误

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

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

最佳答案

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

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

但是

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

当然,为指定定义了适当的编码器

关于mysql - 宏扩展期间出现异常 : [error] java. lang.IllegalStateException:聚合组无效:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59711647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com