光滑的3动态groupby-其列表中的字段[字符串] [英] Slick 3 dynamic groupby - fields from its List[String]

查看:65
本文介绍了光滑的3动态groupby-其列表中的字段[字符串]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我想执行查询:

select columnName1,columnName2,Sum(*)
from table
group by columnName1,columnName2

其中columnName1,columnName2是从list [string]("columnName1",columnName2)提供的

where columnName1,columnName2 is supplied from list[string] ("columnName1",columnName2")

我该如何使用光滑的呢? 如果列在编译时是已知的,那么我可以轻松地使用groupBy函数:

how can i do it with slick? if the columns are known by compile time, i can easily use the groupBy function:

tableQuery.groupBy { r => (r.columnName1,r.columnName2)}.map (case groupName,group) => (groupName._1,groupName._2,group.map(...).avg)}

但是动态呢?

推荐答案

您可以使用r.column("foo")语法在运行时动态访问列,所以我相信您可以执行以下操作?

You can use the r.column("foo") syntax to access columns dynamically at runtime, so I believe you can just do something like the following?

columnNames = List("col1", "col2")
tableQuery
    .groupBy(r => columnNames.map(name => r.column(name)))
    .map({ case (groupName, group) => /* do something with it */})

这篇关于光滑的3动态groupby-其列表中的字段[字符串]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆