Can anybody help me with a query for a count by two categories? I have a set of opinions. Opinions have the value -1,0 or 1. I want the count of all 1 opinions and the count of all -1 opinions. So far I have:
[:find [(count ?pros) (count ?cons)]
:in $ ?id
:where
[?e ::proposal/id ?id]
[?e ::proposal/opinions ?pros] ; 1
[?pros ::opinion/value 1]) ; 2
[?e ::proposal/opinions ?cons] ; 3
[?cons ::opinion/value -1]))] ; 4
This won’t work, because in this query ?pros
and ?cons
would be the same, and 2 and 4 would contradict each other.
I think I do not understand or-join
enough to say if I have to use it.@ben.sless Thanks a lot! The final query:
(d/q
'[:find (clojure.core/frequencies ?values) .
:where
[?e ::proposal/id]
[?e ::proposal/opinions ?opinions]
[?opinions ::opinion/value ?values]]
@db/conn)
@whilo I cannot use frequencies without clojure.core
.
I get:
Execution error (NullPointerException) at datahike.query/-aggregate$fn (query.cljc:907).
null
It seams that Datomic doesn’t need the namespace,to be fair, the datahike docs say you need to provide a fully qualified name. still a gap relative to datomic
Ah. okay. Didn’t see that one.
Don't feel bad 🙂 custom aggregators aren't something you'd expect to find on the front page
(I like asking questions in Slack because of the personal conversations, but Stack Overflow would be better because other people can find and reference the question later)
Write your own aggregator which will behave like frequencies
?
for example this is how max aggregator is implemented:
(fn
[coll]
(reduce (fn [acc x]
(if (pos? (compare x acc))
x acc))
(first coll) (next coll)))
I think you can even just use frequencies
directly