如何获取每个uniqueId的最大时间戳行

时间:2016-05-13 10:39:53

标签: scala slick

我的表格定义如下:

mysql> select * from rules;
+----+----------+-------+--------------+---------------+---------------------+
| id | uniqueId | name  | json         | definition    | createdAt           |
+----+----------+-------+--------------+---------------+---------------------+
|  1 | ABC      | Rule1 | Json Repr v1 | Rule 1 Def v1 | 2016-05-13 09:10:44 |
|  2 | DEF      | Rule2 | Json Repr v1 | Rule 2 Def v1 | 2016-05-13 09:12:04 |
|  3 | ABC      | Rule1 | Json Repr v2 | Rule 1 Def v2 | 2016-05-13 09:14:04 |
|  4 | ABC      | Rule1 | Json Repr v3 | Rule 1 Def v3 | 2016-05-13 09:14:15 |
|  5 | ABC      | Rule1 | Json Repr v4 | Rule 1 Def v4 | 2016-05-13 09:14:22 |
|  6 | DEF      | Rule2 | Json Repr v2 | Rule 2 Def v2 | 2016-05-13 09:14:31 |
+----+----------+-------+--------------+---------------+---------------------+

我知道要运行的SQL查询以获取所需的行:

mysql> select * from rules where createdAt in (select max(createdAt) from rules group by uniqueId);
+----+----------+-------+--------------+---------------+---------------------+
| id | uniqueId | name  | json         | definition    | createdAt           |
+----+----------+-------+--------------+---------------+---------------------+
|  5 | ABC      | Rule1 | Json Repr v4 | Rule 1 Def v4 | 2016-05-13 09:14:22 |
|  6 | DEF      | Rule2 | Json Repr v2 | Rule 2 Def v2 | 2016-05-13 09:14:31 |
+----+----------+-------+--------------+---------------+---------------------+

我试图获得Slick查询,但我遇到了问题。到目前为止,我可以按如下方式构建子查询:

val latestVersionOfRules = Rules.groupBy(p => p.uniqueId).map{ case(uniqueId, group) => (uniqueId, group.map(_.createdAt).max) }

但是,我无法访问createdAt值以便与外部表联接。

我不确定如何构建上面的完整查询。

1 个答案:

答案 0 :(得分:1)

首先:您的查询按原样执行并不是您想要的,因为它只根据查询中的时间戳进行选择。如果您有多个具有最大时间戳的行,则您将选择所有这些行。例如,如果您有:

mysql> select * from rules;
+----+----------+-------+--------------+---------------+---------------------+
| id | uniqueId | name  | json         | definition    | createdAt           |
+----+----------+-------+--------------+---------------+---------------------+
|  1 | ABC      | Rule1 | Json Repr v1 | Rule 1 Def v1 | 2016-05-13 09:10:44 |
|  2 | DEF      | Rule2 | Json Repr v1 | Rule 2 Def v1 | 2016-05-13 09:14:22 |
|  3 | ABC      | Rule1 | Json Repr v2 | Rule 1 Def v2 | 2016-05-13 09:14:04 |
|  4 | ABC      | Rule1 | Json Repr v3 | Rule 1 Def v3 | 2016-05-13 09:14:15 |
|  5 | ABC      | Rule1 | Json Repr v4 | Rule 1 Def v4 | 2016-05-13 09:14:22 |
|  6 | DEF      | Rule2 | Json Repr v2 | Rule 2 Def v2 | 2016-05-13 09:15:31 |
+----+----------+-------+--------------+---------------+---------------------+

请注意,第2行的时间戳等于ABC的最大时间戳。

你会回来:

mysql> select * from rules where createdAt in (select max(createdAt) from rules group by uniqueId);
+----+----------+-------+--------------+---------------+---------------------+
| id | uniqueId | name  | json         | definition    | createdAt           |
+----+----------+-------+--------------+---------------+---------------------+
|  2 | DEF      | Rule2 | Json Repr v1 | Rule 2 Def v1 | 2016-05-13 09:14:22 |
|  5 | ABC      | Rule1 | Json Repr v4 | Rule 1 Def v4 | 2016-05-13 09:14:22 |
|  6 | DEF      | Rule2 | Json Repr v2 | Rule 2 Def v2 | 2016-05-13 09:15:31 |
+----+----------+-------+--------------+---------------+---------------------+

不是你想要的!在SQL中,您将加入一个子选择。这可以在Slick中完成:

// Create the query getting you the unique ID and max.
val latestVersionOfRules = Rules.groupBy(p => p.uniqueId).map {
  case(uniqueId, group) => (uniqueId, group.map(_.createdAt).max)
}

// Join your table to the query. This is a DBIO of whatever your Rules
// result type is.
val results = Rules.join(latestVersionOfRules).on {
  case (rulesRow, groupRow) =>
    rulesRow.uniqueId === groupRow._1 &&
      rulesRow.createdAt === groupRow._2
}.map {
  case (rulesRow, _) => rulesRow
}.result

最后要注意的一点是:如果多行具有相同的最大时间戳,仍将uniqueId行返回多行。