加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何使用光滑进行聚合

发布时间:2020-12-16 09:30:05 所属栏目:安全 来源:网络整理
导读:我想强制光滑创建像 select max(price) from coffees where ... 但slick’s documentation没有帮助 val q = Coffees.map(_.price) //this is query Query[Coffees.type,...]val q1 = q.min // this is Column[Option[Double]]val q2 = q.maxval q3 = q.sumva
我想强制光滑创建像

select max(price) from coffees where ...

但slick’s documentation没有帮助

val q = Coffees.map(_.price) //this is query Query[Coffees.type,...]
val q1 = q.min // this is Column[Option[Double]]
val q2 = q.max
val q3 = q.sum
val q4 = q.avg

因为那些q1-q4不是查询,我无法获得结果,但可以在其他查??询中使用它们。

这个说法

for {
  coffee <- Coffees
} yield coffee.price.max

生成正确的查询但不推荐使用(生成警告:“不推荐使用类ColumnExtensionMethods中的方法max:使用Query.max”)。
如何在没有警告的情况下生成此类查询?

另一个问题是通过以下方式聚合:

"select name,max(price) from coffees group by name"

试图解决它

for {
  coffee <- Coffees
} yield (coffee.name,coffee.price.max)).groupBy(x => x._1)

产生

select x2.x3,x2.x3,x2.x4 from (select x5."COF_NAME" as x3,max(x5."PRICE") as x4 from "coffees" x5) x2 group by x2.x3

这导致明显的db错误

column "x5.COF_NAME" must appear in the GROUP BY clause or be used in an aggregate function

如何生成这样的查询?

解决方法

据我所知,第一个就是简单

Query(Coffees.map(_.price).max).first

第二个

val maxQuery = Coffees
  .groupBy { _.name }
  .map { case (name,c) =>
    name -> c.map(_.price).max
  }

maxQuery.list

要么

val maxQuery = for {
  (name,c) <- Coffees groupBy (_.name)
} yield name -> c.map(_.price).max

maxQuery.list

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读