Datomic查询最大聚合值

时间:2015-02-27 03:07:20

标签: datomic datalog

假设我有实体author,其中有许多相关的book实体。 获取具有最多书籍的作者的查询是什么?

1 个答案:

答案 0 :(得分:2)

行。因为我自己找到了答案 - 我在这里发帖,以防有人搜索:

解决方案是构建两个数据表查询,将第一个输出传递给第二个。

(->>
  (d/q '[:find (count ?b) ?a :where [?a :author/books ?b]] db)
  (d/q '[:find (max ?count) ?a :in $ [?count ?a]] db))

据我所知,这是在数据库中使用较少的简单查询的常用方法 - 将其拆分为多个子查询并链接在一起,让数据库完成其工作。