加入+ postgresql 中每行的计数

发布于 2024-09-27 19:55:19 字数 1699 浏览 1 评论 0原文

当前的“尝试”表:

ID  QUESTION_ID   CORRECT
1   1             FALSE
2   2             TRUE
3   4             FALSE
4   3             FALSE
5   1             TRUE
6   1             TRUE
7   4             TRUE
8   3             TRUE
9   4             FALSE
10   1             TRUE
11   2             TRUE
11   1             FALSE
11   3             FALSE

当前的“问题”表:

ID  ANSWER
1   A
2   A
3   B
4   C
5   C
6   C
7   C

现在我想根据问题的解决次数对问题进行排序。如您所见,问题 1 已解决 3 次,而问题 5、6 和 7 已解决 0 次。 下订单后,我想随机选择解决数量最少的前 5 个问题。

因此,我的问题是:如何做到这一点?我的最终目标是在 Rails 中做到这一点,但首先我想尝试一下 postgresql。那么你知道该怎么做吗?代码示例受到高度赞赏。

问候,

莫里斯

//更新

好吧,我试图将你的建议付诸实践,但我遇到了问题。正如你所看到的,我只得到 1 行,我想是因为 where 子句。难道是我数错了?

=# select q.id, count(q.id)
-# from questions as q
-# left join attempts as a on a.question_id = q.id
-# where a.correct = true and a.user_id = 4
-# group by q.id
-# order by count(q.id) desc
-# limit 20
-# \g
 id  | count 
-----+-------
 112 |     2
(1 row)

// UPDATE 2

好吧,我做了一个嵌套选择来解决这个问题(灵感来自如何从表中 JOIN 一个 COUNT,然后用另一个 JOIN 影响该 COUNT):

select q.id, temp.Count
from questions as q
left join
(select q.id, count(a.id) as count
from questions as q
left join attempts as a
on a.question_id = q.id
where a.correct = true and a.user_id = 4
group by q.id)
temp on temp.id = q.id

给了我一些东西:一个重要的列表。

现在,我仍然需要创建计数最少的随机 5 个问题

我现在尝试用 min(count) 做一些事情,但这似乎不起作用。关于如何做到这一点有什么想法吗?

再次感谢

Current "Attempts" table:

ID  QUESTION_ID   CORRECT
1   1             FALSE
2   2             TRUE
3   4             FALSE
4   3             FALSE
5   1             TRUE
6   1             TRUE
7   4             TRUE
8   3             TRUE
9   4             FALSE
10   1             TRUE
11   2             TRUE
11   1             FALSE
11   3             FALSE

Current "Question" table:

ID  ANSWER
1   A
2   A
3   B
4   C
5   C
6   C
7   C

Now I want to order Questions based on their amount of times solved. As you can see, Question 1 has been solved 3 times while Question 5, 6 and 7 have been solved 0 times.
After I make this order, I want to pick a random top 5 of questions with lowest amount of solved.

Therefore, my questions is: How to do this? My ultimate goal is to do this in rails, but first I want to experiment with postgresql a bit. So do you know how to do this? Code examples highly appreciated.

Regards,

Maurice

// UPDATE

Ok, I tried to put your advice into practise, but im running into a problem. As you can see, i'm just getting 1 row, i think because of the where clause. could it be that im counting it wrong?

=# select q.id, count(q.id)
-# from questions as q
-# left join attempts as a on a.question_id = q.id
-# where a.correct = true and a.user_id = 4
-# group by q.id
-# order by count(q.id) desc
-# limit 20
-# \g
 id  | count 
-----+-------
 112 |     2
(1 row)

// UPDATE 2

ok, i did a nested select to solve this problem (inspired by How to JOIN a COUNT from a table, and then effect that COUNT with another JOIN):

select q.id, temp.Count
from questions as q
left join
(select q.id, count(a.id) as count
from questions as q
left join attempts as a
on a.question_id = q.id
where a.correct = true and a.user_id = 4
group by q.id)
temp on temp.id = q.id

Gave me something: an important list.

Now, I still need to create the random 5 questions that have the least amount of counts.

I'm trying to do something now with min(count), but that doesnt seem to work out. Any ideas on how to do this?

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-10-04 19:55:19
SELECT Q.ID, COUNT(A.ID) AS SolveCount
FROM Question Q
LEFT JOIN Attempts A
ON A.QUESTION_ID = Q.ID
WHERE A.CORRECT = TRUE
GROUP BY Q.ID
ORDER BY COUNT(A.ID)
SELECT Q.ID, COUNT(A.ID) AS SolveCount
FROM Question Q
LEFT JOIN Attempts A
ON A.QUESTION_ID = Q.ID
WHERE A.CORRECT = TRUE
GROUP BY Q.ID
ORDER BY COUNT(A.ID)
有木有妳兜一样 2024-10-04 19:55:19

像下面这样的怎么样?

SELECT Q.ID, COUNT()
FROM Questions AS Q
LEFT JOIN Attempts AS A ON A.QUESTION_ID = Q.ID
WHERE A.CORRECT = TRUE
GROUP BY Q.ID
ORDER BY COUNT() DESC
LIMIT 5

概括:
它将 questions 表连接到 attempts 表,仅考虑正确为 true 的行,按问题 id 分组,按每个问题的真实尝试次数排序,然后限制为前 5 个结果。

我还没有测试过它,但我认为它至少应该接近你正在寻找的东西。

How about somthing like the following?

SELECT Q.ID, COUNT()
FROM Questions AS Q
LEFT JOIN Attempts AS A ON A.QUESTION_ID = Q.ID
WHERE A.CORRECT = TRUE
GROUP BY Q.ID
ORDER BY COUNT() DESC
LIMIT 5

Summary:
It joins the questions table to the attempts table, only considers rows where correct is true, groups by the question id, sorts by the count of true attempts per question, and then limits to the top five results.

I haven't tested it, but I think it should at least be close to what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文