1

我有一张表格,记录了用户对许多问题的回答:

表A

user_id  | question_id | date answered | correct?
-------------------------------------------------
   66         345          timestamp        1
   34         654          timestamp        0
   34         654          timestamp        1

每个用户的每个问题尝试都存储在数据库中。

然后,我还有一个类别列表和该类别中的 question_ids。例如

表B

category_id    |    question_id
--------------------------------
   1                     34
   1                     44
   1                     23
   2                     99 
   2                     44

我正在尝试编写一个查询来计算用户之前正确回答的类别中的correct? = 1问题百分比(其中)以及该类别中最后回答的 20 个问题中正确问题的百分比。

到目前为止,我可以做第一部分,但不能做第二部分

SELECT category_id, COUNT(*), COUNT(correct?)
FROM tableA LEFT JOIN tableB USING (question_id)
WHERE user_id = 1
GROUP_BY category_id

这给了我该类别中的总问题数以及用户在该类别中正确回答的问题数。像这样的东西

cat_id  | total_questions | answered_correctly
-------------------------------------------------
 1           455               323
 2           334               123

但是,对于每个类别,我还想查看一个类别中回答的最后 20 个问题并检索正确的数字。所以我想要这样的东西:

cat_id | total_questions | answered_correctly   | questions_correct_in_last_20_answered
-------------------------------------------------------------------------------------
 1           455                323                            12
 2           334                123                            8
4

3 回答 3

1

嘿,朋友看这个:

Foo.c SELECT, COUNT (*) AS pct * t.factor FROM JOIN (SELECT 100/COUNT (*) FROM foo AS factor) AS t GROUP BY foo.c;

哎呀!因此,进行 JOIN 以获得总用户数并应用一些数学测试就足够了。回到我的实际隐喻情况,我们有:

SELECT count (id) AS pct * t.factor, good_person FROM people
  JOIN (SELECT 100/COUNT (*) FROM persons AS factor) AS t
  GROUP BY good_person;

原始链接(葡萄牙语),此处:MySQL 博客

于 2013-09-20T03:34:05.800 回答
1

要添加最后回答的二十个问题,您需要选择最后二十行,然后计算正确答案,但是GROUP BY不要LIMIT很好地结合在一起,除非您一次只检查一个类别,否则您不能附加最后二十行. 当子查询之一引用正在连接的表时,MySQL 不允许您连接表。

因此,下面的查询是一种解决方法,它获取按时间戳排序的类别的所有答案,制作一个列表,取前 20 个,然后计算正确答案的数量。棘手,但完成工作。

SELECT category_id,
       Total_Q_Tried,
       Total_Unique_Q_Tried,
       Total_Answered_Correctly,
       Total_Answered_Correctly / Total_Q_Tried*100 Total_Correct_Answer_Percentage,
       Total_Answered_Correctly_In_Last20,
       Total_Answered_Correctly_In_Last20 / LEAST(20,Total_Q_Tried)*100 Total_Correct_Answer_Last20_Percentage
FROM (
   SELECT
     B.category_id, COUNT(B.question_id) Total_Q_Tried, 
     COUNT(DISTINCT B.question_id) Total_Unique_Q_Tried,
     SUM(A.correct) Total_Answered_Correctly,

     (SELECT length(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20))
           - length(replace(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20),'1', ''))
      FROM tableA AA INNER JOIN tableB BB ON AA.question_id = BB.question_id
      WHERE BB.category_id = B.category_id
           AND AA.user_id = A.user_id
     ) Total_Answered_Correctly_In_Last20

    FROM tableA A LEFT JOIN tableB B
          ON B.question_id = A.question_id
    WHERE A.user_id = 34
    GROUP BY B.category_id ) FinalNumbers

如果您想要最后 20 个正确答案的百分比,则需要使用 20 中的较小值,TOTAL_Q_TRIED并且TOTAL_ANSWERED_CORRECTLY_IN_LAST20在查询中计算得出。

--

我无法尝试,但如果有很多很多行,性能可能会不好。

| USER_ID | QUESTION_ID |                  DATE_ANSWERED | CORRECT |
|---------|-------------|--------------------------------|---------|
|      66 |           1 | January, 01 2013 00:00:00+0000 |       1 |
|      34 |           1 | January, 02 2013 00:00:00+0000 |       1 |
|      34 |           2 | January, 03 2013 00:00:00+0000 |       1 |
|      34 |           3 | January, 04 2013 00:00:00+0000 |       0 |
|      34 |           4 | January, 05 2013 00:00:00+0000 |       1 |
|      34 |           6 | January, 06 2013 00:00:00+0000 |       0 |


| CATEGORY_ID | QUESTION_ID |
|-------------|-------------|
|           1 |           1 |
|           2 |           2 |
|           2 |           3 |
|           2 |           4 |
|           2 |           5 |
|           3 |           6 |


| CATEGORY_ID | TOTAL_Q_TRIED | TOTAL_UNIQUE_Q_TRIED | TOTAL_ANSWERED_CORRECTLY | TOTAL_CORRECT_ANSWER_PERCENTAGE | TOTAL_ANSWERED_CORRECTLY_IN_LAST20 | TOTAL_CORRECT_ANSWER_LAST20_PERCENTAGE |
|-------------|---------------|----------------------|--------------------------|---------------------------------|------------------------------------|----------------------------------------|
|           1 |             1 |                    1 |                        1 |                             100 |                                  1 |                                    100 |
|           2 |             3 |                    3 |                        2 |                         66.6667 |                                  2 |                                66.6667 |
|           3 |             1 |                    1 |                        0 |                               0 |                                  0 |                                      0 |

根据下面的评论 - 添加正确回答的独特问题的总数。

这变得越来越艰难。我加入每一列,包括添加的最新查询中的时间戳以获得唯一答案。见下文。

SELECT category_id,
       Total_Q_Tried,
       Total_Unique_Q_Tried,
       Total_Answered_Correctly,
       Total_Unique_Answered_Correctly,
       Total_Answered_Correctly / Total_Q_Tried*100 Total_Correct_Answer_Percentage,
       Total_Answered_Correctly_In_Last20,
       Total_Answered_Correctly_In_Last20 / LEAST(20,Total_Q_Tried)*100 Total_Correct_Answer_Last20_Percentage
FROM (
  SELECT
     B.category_id, COUNT(B.question_id) Total_Q_Tried, 
     COUNT(DISTINCT B.question_id) Total_Unique_Q_Tried,
     SUM(A.correct) Total_Answered_Correctly,
     SUM(UniqueA.correct) Total_Unique_Answered_Correctly,


     (SELECT length(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20))
           - length(replace(SUBSTRING_INDEX(GROUP_CONCAT(AA.correct ORDER BY AA.date_answered DESC SEPARATOR ',' ), ',', 20),'1', ''))
      FROM tableA AA INNER JOIN tableB BB ON AA.question_id = BB.question_id
      WHERE BB.category_id = B.category_id
           AND AA.user_id = A.user_id
     ) Total_Answered_Correctly_In_Last20

  FROM tableA A LEFT JOIN tableB B
        ON B.question_id = A.question_id
       LEFT JOIN (select user_id, question_id,  MAX(date_answered) date_answered, correct
                    from tableA
                    GROUP BY user_id, question_id, correct
                   ) UniqueA
        ON A.user_id = UniqueA.user_id AND A.question_id = UniqueA.question_id AND  A.date_answered = UniqueA.date_answered
  WHERE A.user_id = 34
  GROUP BY B.category_id ) FinalNumbers;

对于正确回答的最后 20 个问题的百分比,这可能不正确。请测试一下。如果它不替换,tableA AandtableA AAUniqueA's select 查询仅处理唯一答案并删除添加的最新左连接。

于 2013-09-20T05:02:50.373 回答
0

您需要添加一个返回LIMIT 20按时间戳排序的子查询。

于 2013-09-20T02:58:14.493 回答