我试图使用Slick将查询从SQL转换为Scala代码,但我在filter子句中遇到编译器错误: 构造函数无法实例化为预期类型
我在Slick中的代码:
val subquery = (for {
pit <- PassInTripTable.table
t <- TripTable.table if pit.tripNoFk === t.tripNo
} yield (pit, t))
.map{ case (pit, t) => ( pit, Case.If(t.townFrom <= t.townTo).Then(t.townFrom ++ t.townTo).Else(t.townFrom ++ t.townTo) )}
.groupBy(_._1.idPsgFk)
.filter{ case ((pit, count), group) => ( group.map(_._2).countDistinct === 1)}
.map(_._1)
val query = PassengerTable.table.filter(_.idPsg in subquery).map(_.name)
db.run(query.result)
SQL本身的查询:
select name from passenger
where id_psg in
(
select id_psg from trip t,pass_in_trip pit
where t.trip_no=pit.trip_no
group by id_psg
having count(distinct case when town_from<=town_to then town_from+town_to else town_to+town_from end)=1
)
如果有人帮我找错,我将非常感激。
答案 0 :(得分:1)
通过查看您的代码,看起来您匹配的类型不应该是&#34;((坑,计数),组)&#34;。
在Slick中的groupBy只返回Tuple2s的集合。
http://slick.lightbend.com/doc/3.0.0/queries.html
因此,过滤器可能看起来像......
.filter{ case (pit, count) => ( count.map(_._2).countDistinct === 1)}