【发布时间】:2019-11-20 20:03:52
【问题描述】:
我在 SQLITE 数据库中有三个表,分别称为 Needle、NeedleHaystack 和 Haystack,它们用于多对多关系连接表。我需要计算 needle 中每个独特项目的频率,它在每个独特 Haystack 中出现的频率(最好是百分比,计数是可以接受的)。
Needle NeedleHaystack Haystack
+----+-------+ +----+-----------+-------------+ +----+-------+
| id | value | | id | needle_id | haystack_id | | id | value |
+----+-------+ +----+-----------+-------------+ +----+-------+
| 1 | foo1 | | 1 | 1 | 7 | | 7 | bar7 |
| 2 | foo2 | | 2 | 1 | 8 | | 8 | bar8 |
| 3 | foo3 | | 3 | 1 | 9 | | 9 | bar9 |
+----+-------+ | 4 | 2 | 7 | +----+-------+
+----+-----------+-------------+
这样我们最终会得到这样的结果
+-----------+--------------------------+
| needle_id | frequency_over_haystacks |
+-----------+--------------------------+
| 1 | 100% | // needle id 1 appears in 100% of Haystacks
| 2 | 33% | // needle id 2 appears in 33% of Haystacks
| 3 | 0% | // needle id 3 appears in no Haystacks
+-----------+--------------------------+ // on and on, for each needle that may be present...
【问题讨论】:
-
为什么针 1 的百分比不是 100%?
-
很好,已修复。