最大光滑

时间:2017-11-14 14:02:22

标签: sql scala aggregate-functions slick

我正在学习如何使用Slick,我需要从SQL构建这样的查询:

  select TOP 1 WITH TIES name, c3 from passenger
    join
          (select c1, max(c3) c3 from
            (select pass_in_trip.ID_psg c1, Trip.ID_comp c2, count(*) c3 from pass_in_trip
            join trip on trip.trip_no=pass_in_trip.trip_no
            group by pass_in_trip.ID_psg, Trip.ID_comp
            ) as t

      group by c1
      having count(*)=1) as tt

on ID_psg=c1
order by c3 desc

到目前为止我取得的成就:

val t = (for {
      pit <- PassInTripTable.table
      t <- TripTable.table if pit.tripNoFk === t.tripNo
    } yield (pit, t))
      .groupBy{case (pit,t) => (pit.idPsgFk, t.idCompFk)}
        .map{case ((idPsgFk, idCompFk), group) => (idPsgFk, idCompFk, group.length)}

    val tt = t.filter{case (idPsgFk, idCompFk, count) => count === 1}
      .groupBy(_._1)
      .map(_._1)

    val query = (for {
      p <- PassengerTable.table
      t <- tt if p.idPsg === t
    } yield (p, t))
    .map{ case (p,t) => p.name}
     .take(1)

唯一的问题是在最终查询中我需要选择最大值,而我无法弄清楚如何将其应用于我的查询。如果有人能帮助我,我将非常感激!

0 个答案:

没有答案