我正在尝试形成一个查询以生成以下直方图。
假设实体动物,具有无限数量的标签。
dog.features = [blue, big, fury, nice]
cat.features = [white, big, fury, ugly]
bear.features = [white, big, fury, ugly]
other_bear.features = [white, big, fury, ugly]
想象一下,我想要生成 cat 和其他类似特征的计数直方图。
首先,我们需要计算所有有猫的夫妻的相似特征
similar_features(cat, dog) = 2
similar_features(cat, bear) = 4
similar_features(cat, other_bear) = 4
那么,相似特征的直方图将是:
|0| = 0
|1| = 0
|2| = 1
|3| = 0
|4| = 2
我想用 JPQL 来实现这一点 - 我是这样开始的:
SELECT COUNT(DISTINCT feature1) cnt, COUNT(COUNT(DISTINCT feature1))
FROM Animal a1, Animal a2
JOIN a1.features feature1 JOIN a2.features feature2
WHERE feature1 = feature2 AND i2.id = ?1
GROUP BY a1, cnt
这显然是错误的,但它显示了我想要实现的目标。JPQL 甚至可以做到这一点吗?