SQL 查询帮助 - 多项选择测试评分

发布于 2024-07-04 10:34:34 字数 309 浏览 5 评论 0原文

假设我有一个 Student 表,它有一个 int ID。 我有一组固定的 10 个多项选择题,有 5 个可能的答案。 我有一个标准化答案表,其中包含问题 id、Student.answer (1-5) 和 Student.ID

我正在尝试编写一个查询,该查询将返回超过特定百分比的所有分数。 为此,我编写了一个简单的 UDF,它接受 Student.answers 和正确答案,因此它有 20 个参数。

我开始想知道是否最好对答案表进行非规范化,将其带入我的应用程序并让我的应用程序进行评分。

有人曾经处理过这样的事情并且有洞察力吗?

Say I have a Student table, it's got an int ID. I have a fixed set of 10 multiple choice questions with 5 possible answers. I have a normalized answer table that has the question id, the Student.answer (1-5) and the Student.ID

I'm trying to write a single query that will return all scores over a certain pecentage. To this end I wrote a simple UDF that accepts the Student.answers and the correct answer, so it has 20 parameters.

I'm starting to wonder if it's better to denormalize the answer table, bring it into my applcation and let my application do the scoring.

Anyone ever tackle something like this and have insight?

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

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

发布评论

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

评论(6

五里雾 2024-07-11 10:34:34

从长远来看,您正在谈论的架构可能会变得非常麻烦,如果您需要更改问题,则意味着您正在使用的 UDF 需要进行更多更改。

我认为您可能可以在代码中进行分析,而不必对数据库进行反规范化。 非规范化还可能导致不灵活,或者至少增加未来的更新费用。

The architecture you are talking about could become very cumbersome in the long run, and if you need to change the questions it means more changes to the UDF you are using.

I would think you could probably do your analysis in code without necessarily de-normalizing your database. De-normalization could also lend to inflexibility, or at least added expense to update, down the road.

我不会写诗 2024-07-11 10:34:34

如果我正确理解你的模式和问题,那么这样的事情怎么样:

select student_name, score
from students
  join (select student_answers.student_id, count(*) as score
        from student_answers, answer_key
        group by student_id
        where student_answers.question_id = answer_key.question_id
          and student_answers.answer = answer_key.answer)
  as student_scores on students.student_id = student_scores.student_id
where score >= 7
order by score, student_name

例如,应该选择分数为 7 或更高的学生。 只需根据您的目的调整 where 子句即可。

If I understand your schema and question correctly, how about something like this:

select student_name, score
from students
  join (select student_answers.student_id, count(*) as score
        from student_answers, answer_key
        group by student_id
        where student_answers.question_id = answer_key.question_id
          and student_answers.answer = answer_key.answer)
  as student_scores on students.student_id = student_scores.student_id
where score >= 7
order by score, student_name

That should select the students with a score of 7 or more, for example. Just adjust the where clause for your purposes.

许久 2024-07-11 10:34:34

我可能会将其留给您的应用程序来执行评分。 查看 Jeff Atwood 的也许标准化并不正常

I would probably leave it up to your application to perform the scoring. Check out Maybe Normalizing Isn't Normal by Jeff Atwood.

辞旧 2024-07-11 10:34:34

这个查询应该很容易......假设您在问题表中存储了正确的答案。 你确实在问题表中存储了正确答案,对吗?

This query should be quite easy... assuming you have the correct answer stored in the question table. You do have the correct answer stored in the question table, right?

雨的味道风的声音 2024-07-11 10:34:34

非规范化通常被认为是最后的手段。 这个问题似乎与很常见的调查应用程序非常相似。 如果没有看到你的数据模型,很难提出解决方案,但我会说这绝对是可能的。 我想知道为什么该函数需要 20 个参数?

在大多数情况下,基于关系集的解决方案会更简单、更快。

Denormalization is generally considered a last resort. The problem seems very similar to survey applications, which are very common. Without seeing your data model, it's difficult to propose a solution, but I will say that it is definitely possible. I'm wondering why you need 20 parameters to that function?

A relational set-based solution will be simpler and faster in most cases.

ぺ禁宫浮华殁 2024-07-11 10:34:34

没办法,你肯定想保持正常化。 这甚至不是那么难的查询。

基本上,您需要将学生的正确答案与该问题的总答案连接起来,然后进行计数。 这将为您提供正确的百分比。 对每个学生都这样做,并将最小正确百分比放入 where 子句中。

No way, you definitely want to keep it normalized. It's not even that hard of a query.

Basically, you want to left join the students correct answers with the total answers for that question, and do a count. This will give you the percent correct. Do that for each student, and put the minimum percent correct in a where clause.

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