现在有一张表保存了各个用户答题的答案,有这么几列,考试id,问题id,考生id,现在要要统计答题错误率,就是说要有这么4列,第一列是问题id,第二列是这个问题的答题数量(比如这个问题在所有考试中一共出现10次),第三列是答错的数量(比如说10次中一共有5次打错),第四列就是错误率(6/10=60%),现在的问题就是第二列和第三列我是通过count出来的,那么如何得到第四列的数据
SELECT question_id,COUNT(question_id) AS www,1.0 * sum(CASE
WHEN full_score<>score THEN 1
ELSE 0
END)/COUNT(question_id) AS yyy
FROM table1
GROUP BY question_id
ORDER BY question_id
SELECT question_id,COUNT(question_id) AS www,
COUNT(CASE WHEN full_score<>score THEN 1 ELSE NULL END) AS yyy,
1.0 * sum(CASE WHEN full_score<>score THEN 1 ELSE 0 END)/COUNT(question_id) AS zzz
FROM table1
GROUP BY question_id
ORDER BY question_id
一共是4列,我就想知道是不是只能是按照上面这种写法,感觉很麻烦,就不能直接用yyy/www这样简单的写法吗?或者您可以提供另外一套思路吗?
我就是想将题号,某道题出现的总数、该题打错数,错误率这些数据显示出来,您有没有比较好的解决方法。
@渔网网鱼:
select *,yyy/www as zzz from (
SELECT question_id,
COUNT(question_id) AS www,
1.0 * sum(CASE WHEN full_score<>score THEN 1 ELSE 0 END) yyy
FROM table1 GROUP BY question_id ORDER BY question_id
) as t1
最简单的方法是直接相除,不过好像不是60%,而是50%吧
select 1.00*count(1)/count(ID) from tb
大哥,小弟眼拙啊,你这什么意思说清楚一点啊,我把代码发给你看下,你看下具体怎么改
SELECT id,COUNT(question_id) AS www,COUNT(CASE
WHEN full_score<>score THEN 1
ELSE NULL
END) AS yyy
FROM table1,table2,table3
WHERE question_id=id
GROUP BY id
ORDER BY id
希望得到表的信息,才能进行分析……
答案表
主要是这张表,question_id是可以重复的,然后full_score和score一样的话证明答对,否则打错,查询语句可以看一下上面